本記事は、以下公式サイトを参考としています。
基本的な使い方がまだ知らない人は先に以下をご参照ください。
Display a List of Amazon S3 Buckets
デフォルトのプロファイルの場合
import boto3 # Create an S3 client s3 = boto3.client('s3') # Call S3 to list current buckets response = s3.list_buckets() # Get a list of all bucket names from the response buckets = [bucket['Name'] for bucket in response['Buckets']] # Print out the bucket list print("Bucket List: %s" % buckets)
プロファイルを指定したい場合
- profile に指定したいプロファイルを指定してください。
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_buckets() # Get a list of all bucket names from the response buckets = [bucket['Name'] for bucket in response['Buckets']] # Print out the bucket list print("Bucket List: %s" % buckets)
コメント