Python unzipping stream of bytes?

前端 未结 4 707
时光取名叫无心
时光取名叫无心 2020-11-27 15:34

Here is the situation:

  • I get gzipped xml documents from Amazon S3

    import boto
    from boto.s3.connection import S3Connection
    from boto.s3.key i         
    
    
            
4条回答
  •  眼角桃花
    2020-11-27 16:22

    For Python3x and boto3-

    So I used BytesIO to read the compressed file into a buffer object, then I used zipfile to open the decompressed stream as uncompressed data and I was able to get the datum line by line.

    import io
    import zipfile
    import boto3
    import sys
    
    s3 = boto3.resource('s3', 'us-east-1')
    
    
    def stream_zip_file():
        count = 0
        obj = s3.Object(
            bucket_name='MonkeyBusiness',
            key='/Daily/Business/Banana/{current-date}/banana.zip'
        )
        buffer = io.BytesIO(obj.get()["Body"].read())
        print (buffer)
        z = zipfile.ZipFile(buffer)
        foo2 = z.open(z.infolist()[0])
        print(sys.getsizeof(foo2))
        line_counter = 0
        for _ in foo2:
            line_counter += 1
        print (line_counter)
        z.close()
    
    
    if __name__ == '__main__':
        stream_zip_file()
    

提交回复
热议问题