LoginSignup
9

More than 5 years have passed since last update.

boto を使って、S3 からファイルをダウンロードする。

Posted at

boto.connect_s3 から connection とれて、そこからバケットとれて、key までとれる、便利。

バージョン

  • Python 2.7.2
  • boto (2.27.0)

コード

from cStringIO import StringI

from boto import connect_s3

# token
access_key = ''
secret_key = ''

# bucket, key
bucket_name = ''
file_key = ''

# S3 からファイルを取得
conn = connect_s3(access_key, secret_key)
bucket = conn.get_bucket(bucket_name)
key = bucket.get_key(file_key)

# key_name で取得できなかった場合 None が返る
if not key:
    return u'ないよー'

# ファイルダウンローして、 StgingIO に書き込み
fp = StringIO()
key.get_contents_to_file(fp)
fp.seek(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
9