Skip to content

Instantly share code, notes, and snippets.

@a-hisame
Created May 17, 2016 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-hisame/f90815f4fae695ad3f16cb48a81ec06e to your computer and use it in GitHub Desktop.
Save a-hisame/f90815f4fae695ad3f16cb48a81ec06e to your computer and use it in GitHub Desktop.
Amazon S3と永続化ファイルを経由せずにjson.gzを取り扱う (boto3を利用)
#!/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'あ')
@Kisung
Copy link

Kisung commented Nov 17, 2016

Nice!!! Very Useful....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment