Created
October 1, 2014 15:16
-
-
Save rtoal/30d58a3d18ef040255de to your computer and use it in GitHub Desktop.
Read gzipped JSON file from S3
This file contains 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 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