Created
May 17, 2016 07:27
-
-
Save a-hisame/f90815f4fae695ad3f16cb48a81ec06e to your computer and use it in GitHub Desktop.
Amazon S3と永続化ファイルを経由せずにjson.gzを取り扱う (boto3を利用)
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import json | |
import gzip | |
import StringIO | |
import boto3 | |
def upload(bucket, key, obj): | |
s3 = boto3.client('s3') | |
inmem = StringIO.StringIO() | |
with gzip.GzipFile(fileobj=inmem, mode='wb') as fh: | |
fh.write(json.dumps(obj)) | |
inmem.seek(0) | |
s3.put_object(Bucket=bucket, Body=inmem, Key=key) | |
def download(bucket, key, else_value=None): | |
s3 = boto3.client('s3') | |
response = s3.get_object(Bucket=bucket, Key=key) | |
content = response['Body'].read() | |
with gzip.GzipFile(fileobj=StringIO.StringIO(content)) as fh: | |
try: | |
return json.loads(fh.read()) | |
except Exception as e: | |
return else_value | |
if __name__ == '__main__': | |
BUCKET_NAME = '' | |
KEY_NAME = '' | |
upload(BUCKET_NAME, KEY_NAME, { u'あ': u'いうえお' }) | |
obj = download(BUCKET_NAME, KEY_NAME) | |
print obj.get(u'あ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!!! Very Useful....