View a markdown version of this page

Work with Amazon S3 buckets - AWS SDK for Python v4

Developer Preview: This documentation covers the AWS SDK for Python v4, which is in Developer Preview and intended for evaluation and testing only. Do not use it for production workloads. For production applications, use the AWS SDK for Python (Boto3). To understand the differences between the two SDKs, see Choosing the right AWS SDK for Python.

Work with Amazon S3 buckets

A bucket is a container for objects. You create each general purpose bucket in one AWS Region. Its name must be unique across all AWS accounts in the partition. For the naming rules, see General purpose bucket naming rules in the Amazon Simple Storage Service User Guide. The examples name each bucket python-example-bucket- followed by a random UUID, so that the name is unique.

Before you begin

The examples on this page require the following:

Create a bucket

For request and response details, see the create_bucket() API reference.

Use create_bucket to create a bucket in the client's Region. Outside the US East (N. Virginia) Region, pass the Region as the location_constraint in the create_bucket_configuration dictionary. In us-east-1, omit the configuration, because Amazon S3 rejects a us-east-1 location constraint.

Imports

import uuid from aws_sdk_s3 import S3Client

Code

def create_bucket(client: S3Client, region: str) -> str: """Create a bucket with a unique name and return the name.""" bucket_name = f"python-example-bucket-{uuid.uuid4()}" if region == "us-east-1": client.create_bucket(bucket=bucket_name) else: client.create_bucket( bucket=bucket_name, create_bucket_configuration={"location_constraint": region}, ) return bucket_name

Pass the Region that the client uses. When you create the client from a resolved configuration, that Region is config.region.

config = S3Config.resolve() if config.region is None: raise ValueError("No AWS Region is configured") with S3Client(config=config) as client: bucket_name = create_bucket(client, config.region)

List buckets

For request and response details, see the list_buckets() API reference.

Use list_buckets to list the general purpose buckets that your account owns in all Regions. Set max_buckets to request the buckets one page at a time. Continue with continuation_token until the response contains no token. Each Bucket model in a page has the bucket's name, creation_date, and bucket_region.

Imports

from aws_sdk_s3 import S3Client from aws_sdk_s3.models import Bucket

Code

def list_buckets(client: S3Client) -> list[Bucket]: """Return every general purpose bucket that the account owns.""" buckets: list[Bucket] = [] continuation_token = None while True: response = client.list_buckets( max_buckets=1000, continuation_token=continuation_token ) buckets.extend(response.buckets or []) continuation_token = response.continuation_token if not continuation_token: return buckets

Delete a bucket

For request and response details, see the delete_bucket() API reference.

A bucket must be empty before you can delete it. If the bucket still contains objects, Amazon S3 rejects the request with a BucketNotEmpty error. To delete the objects and then the bucket, see Clean up the example bucket.

Imports

from aws_sdk_s3 import S3Client

Code

def delete_bucket(client: S3Client, bucket_name: str) -> None: """Delete an empty bucket.""" client.delete_bucket(bucket=bucket_name)

More information