0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PythonでAWS S3のバケット内のファイルをすべてダウンロードする方法

Last updated at Posted at 2021-05-27

動作環境

  • python 3.9

Boto3のインストール

pip install boto3

前準備

ドキュメントを参考にAWSのアクセスキーなどを設定する。
アクセスキーを設定しなくてもpythonのコード内に記載し、アクセスすることもできる。

コード例

├── main.py
└── objects

main.py
from boto3.session import Session


def download_objects():
    # 上記の前準備でアクセスキーなどの設定をしなかった場合に必要。
    session = Session(aws_access_key_id='アクセスキー',
                      aws_secret_access_key='シークレットキー',
                      region_name='リージョン名')

    s3 = session.resource('s3')
    bucket = s3.Bucket('s3のバケット名')
    image_objects = bucket.objects.all()
    for image_object in image_objects:
        # 第2引数に保存先のパスを指定します。
        bucket.download_file(image_object.key, f"./object/{image_object.key}")


if __name__ == '__main__':
    download_objects()
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?