Created
May 17, 2016 07:27
-
-
Save a-hisame/f90815f4fae695ad3f16cb48a81ec06e to your computer and use it in GitHub Desktop.
Revisions
-
a-hisame created this gist
May 17, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ #!/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'あ')