Skip to content

Instantly share code, notes, and snippets.

@tokenmathguy
Created July 14, 2016 20:22
Show Gist options
  • Save tokenmathguy/10d1a7076f7646000fef7fc6a30bd997 to your computer and use it in GitHub Desktop.
Save tokenmathguy/10d1a7076f7646000fef7fc6a30bd997 to your computer and use it in GitHub Desktop.
import boto3
import gzip
from StringIO import StringIO
def get_contents_as_string(bucket, key_prefix,
aws_access_key_id=None, aws_secret_access_key=None):
"""Gets the contents as string from a gzipped file on s3
Yeah, it's all in memory, so not a good solution for large objects.
Args:
bucket: s3 bucket
key_prefix: key prefix
aws_access_key_id: optional aws access key id
aws_secret_access_key: optional aws secret access key
Returns:
string of decompressed contents of the s3 file
"""
if aws_access_key_id is None or aws_secret_access_key is None:
cnn = boto3.resource('s3')
else:
cnn = boto3.resource('s3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key)
obj = cnn.Object(bucket, key_prefix).get()
gz_content = obj["Body"].read()
return gzip.GzipFile(fileobj=StringIO(gz_content)).read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment