Amazon S3 Buckets

An Amazon S3 bucket is a storage location to hold files. S3 files are referred to as objects.

This section describes how to use the AWS SDK for Python to perform common operations on S3 buckets.

Create an Amazon S3 Bucket

The name of an Amazon S3 bucket must be unique across all regions of the AWS platform.

import logging
import boto3
from botocore.exceptions import ClientError


def create_bucket(bucket_name):
""" Create an Amazon S3 bucket

:param bucket_name: Unique string name
:return: True if bucket is created, else False
"""

s3 = boto3.client('s3')
try
    s3.create_bucket(Bucket=bucket_name)
except ClientError as e:
    logging.error(e)
    return False
return True

List Existing Buckets

List all the existing buckets for the AWS account.

# Retrieve the list of existing buckets
s3 = boto3.client('s3')
response = s3.list_buckets()

# Output the bucket names
print('Existing buckets:')
for bucket in response['Buckets']:
    print(f'  {bucket["Name"]}')