Created
May 2, 2019 15:54
-
-
Save viesti/91c711a6ab1881f87337f8af713b6133 to your computer and use it in GitHub Desktop.
Stream and decompress a gzip file from S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import zlib | |
def decompress_object(bucket, key, out_file): | |
s3 = boto3.client("s3") | |
body = s3.get_object(Bucket=bucket, Key=key)['Body'] | |
with open(out_file, "ab") as out: | |
decompressor = zlib.decompressobj(zlib.MAX_WBITS|32) | |
for chunk in body.iter_chunks(): | |
out.write(decompressor.decompress(chunk)) | |
decompressor.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment