Skip to content

Instantly share code, notes, and snippets.

@rtoal
Created October 1, 2014 15:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtoal/30d58a3d18ef040255de to your computer and use it in GitHub Desktop.
Save rtoal/30d58a3d18ef040255de to your computer and use it in GitHub Desktop.
Read gzipped JSON file from S3
import json
import gzip
import cStringIO
from boto.s3.connection import S3Connection
from boto.s3.key import Key
AWSACCESSKEY='********************'
AWSSECRETKEY='****************************************'
def read_gzipped_json_file_from_s3(bucket_name, key_name):
conn = S3Connection(AWSACCESSKEY, AWSSECRETKEY)
bucket = conn.get_bucket(bucket_name)
key = bucket.get_key(key_name)
if key is None:
sys.stderr.write('Can\'t get the data from S3\n')
return None
stream = cStringIO.StringIO()
key.get_file(stream)
stream.seek(0)
everything = json.loads(gzip.GzipFile(fileobj=stream).read())
return {k:v for k,v in everything.iteritems() if v}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment