2016-10-23 5 views
2

botocore 및 aiohttp 서비스를 사용하여 S3에 업로드 된 파일의 콘텐츠를 가져오고 싶습니다. 파일이 거대한 크기있을 수 있습니다으로 : aiobotocore-aiohttp - S3 파일 콘텐츠 가져 오기 및 응답에서 스트림

  • 내가 S3에서 파일을 다운로드하는 동안 다른 요청을 처리 할 수 ​​있도록하려면 내가 메모리에 전체 파일 내용을 저장하지 않으

    • 을, (aiobotocore, aiohttp),
    • 는 내가 다운로드 파일에 대한 수정 사항을 적용 할 수 있도록하려면, 그래서 라인으로 라인을 치료하고 지금은 클라이언트

    에 대한 응답을 스트리밍 할, 나는 코드를 다음 한 내 aiohttp 처리기 :

    import asyncio         
    import aiobotocore        
    
    from aiohttp import web       
    
    @asyncio.coroutine        
    def handle_get_file(loop):      
    
        session = aiobotocore.get_session(loop=loop) 
    
        client = session.create_client(    
         service_name="s3",      
         region_name="",       
         aws_secret_access_key="",    
         aws_access_key_id="",     
         endpoint_url="http://s3:5000"   
        )           
    
        response = yield from client.get_object( 
         Bucket="mybucket",      
         Key="key",        
        )           
    

    주어진 파일에서 한 줄을 읽을 때마다 응답을 보내려고합니다. 실제로 get_object()는 Body (ClientResponseContentProxy 객체)가있는 dict을 내부에 반환합니다. read() 메서드를 사용하여 예상되는 응답을 청크로 가져와 클라이언트에 스트리밍 할 수 있습니까?

    내가 할 경우 :

    for content in response['Body'].read(10): 
        print("----")       
        print(content)   
    

    루프 내부의 코드가 실행되지 않습니다.

    하지만 수행 할 때

    result = yield from response['Body'].read(10) 
    

    나는 결과에서 파일의 내용을 얻는다. read() 메서드를 사용하는 방법에 대해 약간 혼란 스럽습니다.

    감사

  • 답변

    1

    aiobotocore API를 read() 당신이

    이 (https://github.com/aio-libs/aiobotocore/pull/19에서 촬영)이 같이 보입니다

    에서 양보해야하는 대한 FlowControlStreamReader.read 발전기를 돌려 여기, 하나의 botocore의 다릅니다 있기 때문에
    resp = yield from s3.get_object(Bucket='mybucket', Key='k') 
    stream = resp['Body'] 
    try: 
        chunk = yield from stream.read(10) 
        while len(chunk) > 0: 
         ... 
         chunk = yield from stream.read(10) 
    finally: 
        stream.close() 
    

    실제로 귀하의 경우에 당신도 사용할 수 있습니다 readline()

    https://github.com/KeepSafe/aiohttp/blob/c39355bef6c08ded5c80e4b1887e9b922bdda6ef/aiohttp/streams.py#L587

    +0

    감사합니다. – jean553

    관련 문제