Created
February 20, 2018 22:23
-
-
Save sminot/a5b481a228bf3d133fa7b0a877c0c0e0 to your computer and use it in GitHub Desktop.
Read JSON directly from AWS 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 io | |
import json | |
import gzip | |
import boto3 | |
def read_gzipped_json_file_from_s3(bucket_name, key_name): | |
s3 = boto3.client('s3') | |
retr = s3.get_object(Bucket=bucket_name, Key=key_name) | |
bytestream = io.BytesIO(retr['Body'].read()) | |
got_text = gzip.GzipFile(None, 'rb', fileobj=bytestream).read().decode('utf-8') | |
dat = json.loads(got_text) | |
return dat | |
def listdir_s3(bucket_name, prefix): | |
out = !aws s3 ls $bucket_name/$prefix | sed 's/.* //' | |
for line in out: | |
yield line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment