How to create an S3 Bucket using Python boto3

To create an S3 Bucket in your target AWS Account, you will need to use the create_bucket() method of boto3 S3.

The method requires only the parameter Bucket, which is your target bucket name.

But I highly recommend that you also use the CreateBucketConfiguration parameter to set the region of the S3 Bucket. If you do not set the CreateBucketConfiguration parameter, it will create your S3 Bucket in the N. Virginia region (us-east-1) by default.

Below are two ways to create an S3 Bucket using Python boto3.

Both python scripts does the same thing. They will create an S3 Bucket named radishlogic-bucket in the Singapore region (ap-southeast-1).

You can choose whichever method you are comfortable with.

Example 1: Code for creating an S3 Bucket using boto3 S3 client

import boto3

# Defining function to create an S3 Bucket
def create_s3_bucket(bucket_name, region):

    # Initialize boto3 S3 Client
    s3_client = boto3.client('s3')

    # Create the S3 Bucket
    s3_client.create_bucket(
        Bucket=bucket_name,
        CreateBucketConfiguration={
            'LocationConstraint': region
        }
    )


# Calling function to create S3 Bucket
create_s3_bucket(
    bucket_name='radishlogic-bucket',
    region='ap-southeast-1'
)

Example 2: Code for creating an S3 Bucket using boto3 S3 resource

import boto3

# Defining function to create an S3 Bucket
def create_s3_bucket(bucket_name, region):

    # Initialize boto3 S3 resource
    s3_resource = boto3.resource('s3')

    # Create resource representing the S3 bucket
    s3_bucket = s3_resource.Bucket(name=bucket_name)

    # Create the S3 bucket
    s3_bucket.create(
        CreateBucketConfiguration={
            'LocationConstraint': region
        }
    )


# Calling defined function to create S3 Bucket
create_s3_bucket(
    bucket_name='radishlogic-bucket',
    region='ap-southeast-1'
)

s3_bucket.create() method uses the create_bucket() of boto3.client(‘s3’) in the background.


We hope this helps you create an S3 Bucket in your AWS Account using Python and boto3.

Let us know your experience in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.