LoginSignup
1
1

More than 5 years have passed since last update.

Python3でバイト型データをs3にgzipで圧縮してアップロード

Posted at

Python3での例があまりなかったので。
やってみたコードを適当に真似て書いただけなので適宜置き換えてください。

import io
import gzip
from boto.s3.connection import S3Connection
from boto.s3.key import Key

def upload(data):  # dataはバイト型のデータ
    stream = io.BytesIO()
    gzip_file = gzip.GzipFile(fileobj=stream, mode='w')
    gzip_file.write(data)
    gzip_file.close()
    conn = S3Connection(AWS_ACCESS_ID, AWS_SECRET_KEY, host=S3_HOST)
    bucket = conn.get_bucket('bucket_name')
    k = Key(bucket, 'key_name')
    k.set_contents_from_string(stream.getvalue(), headers={"Content-Type": 'application/x-gzip'})
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1