Feedback
Do you have a suggestion to improve this website or boto3?Give us feedback.
An Amazon S3 bucket is a storage location to hold files. S3 files are referredto as objects.
This section describes how to use the AWS SDK for Python to perform commonoperations on S3 buckets.
The name of an Amazon S3 bucket must be unique across all regions of the AWSplatform. The bucket can be located in a specific region to minimize latencyor to address regulatory requirements.
importloggingimportboto3frombotocore.exceptionsimportClientErrordefcreate_bucket(bucket_name,region=None):"""Create an S3 bucket in a specified region If a region is not specified, the bucket is created in the S3 default region (us-east-1). :param bucket_name: Bucket to create :param region: String region to create bucket in, e.g., 'us-west-2' :return: True if bucket created, else False """# Create buckettry:ifregionisNone:s3_client=boto3.client('s3')s3_client.create_bucket(Bucket=bucket_name)else:s3_client=boto3.client('s3',region_name=region)location={'LocationConstraint':region}s3_client.create_bucket(Bucket=bucket_name,CreateBucketConfiguration=location)exceptClientErrorase:logging.error(e)returnFalsereturnTrue
List all the existing buckets for the AWS account.
# Retrieve the list of existing bucketss3=boto3.client('s3')response=s3.list_buckets()# Output the bucket namesprint('Existing buckets:')forbucketinresponse['Buckets']:print(f'{bucket["Name"]}')