基本的な使い方がまだ知らない人は先に以下をご参照ください。
Display Bucket Contents
バケット内のファイル、ディレクトリを取得する
- profile にプロファイルを指定してください。
- Bucket にバケット名を指定してください。
- Prefix には、バケット内で出力したいフォルダなどを指定してください。
何も指定しない場合は、バケット内のrootから出力されます。
# -*- coding: utf-8 -*- from boto3.session import Session profile = 'sample-profile' session = Session(profile_name=profile) # Create an S3 client s3 = session.client('s3') # Call S3 to list current buckets response = s3.list_objects( Bucket='sample-bucket', Prefix='' ) if 'Contents' in response: keys = [content['Key'] for content in response['Contents']] print(keys)
ちょっと見やすく表示
# -*- coding: utf-8 -*- from boto3.session import Session profile = 'sample-profile' session = Session(profile_name=profile) # Create an S3 client s3 = session.client('s3') # Call S3 to list current buckets response = s3.list_objects( Bucket='sample-bucket', Prefix='' ) if 'Contents' in response: keys = [content['Key'] for content in response['Contents']] for key in keys: print(key)
コメント