How to create AWS S3 Buckets using Python and AWS CLI

The AWS CLI and Python are two of the most popular tools for managing Amazon Web Services. In this series of posts, I’ll show you how easy it is to work with S3 using both AWS CLI and Python.
The AWS CLI can be installed with a simple pip install awscli command, while python-boto3 is required for accessing the AWS API from python. If you need help in installing AWS CLI and Python on your machine you can read these blogs. This blog expects that you have AWS CLI and Python configured on your machine.

Create S3 bucket using AWS CLI

AWS CLI offers 2 ways to create S3 buckets. One way is using aws s3api commands and another is using aws s3 commands.

The aws s3 has a custom set of commands designed to make working with S3 easier. These are built on top of aws s3api commands.

Using s3api to create an S3 bucket

Let us start with s3api and check what options we have to create an S3 bucket.

I have used double quotes to show values for bucket name in this command. This is not mandatory but I like to use it to add readability to CLI commands.

This command will create “s3-bucket-from-cli-1”. We can check this in the AWS S3 console.

S3 console showing AWS CLI has created a bucket
S3 console showing AWS CLI has created a bucket

We can check that this command has created a bucket in S3 and this is located in the US East 1 region. An S3 bucket will be created in the same region that you have configured as the default region while setting up AWS CLI. We can pass parameters to create a bucket command if you want to change that region and access policy while creating a bucket.

Using s3 mb command to create a bucket

As mentioned earlier we can also create an S3 bucket using s3 commands API. In this case, we have to pass the bucket name as s3 URI instead of plain text.

Creating S3 bucket using python

Now we will write python code to create an S3 bucket. Just like CLI python offers multiple ways to create an S3 bucket. Let us check one by one.

Using boto3 s3 client to create a bucket

Below is code that will create a bucket in aws S3. Like with CLI we can pass additional configurations while creating bcuket.

Let us verify that buckets have been created in S3.

S3 console screenshot showing python s3 client has created buckets.
Python s3 client create bucket output

We can see after running the above code we have 2 new buckets in the s3 console. Also for bucket 2 region is set to ap-south-1 as we have specified during create bucket call.

Using s3 resource object to create a bucket

Just like the s3 client, we can also use s3 resource to create an S3 bucket.

The process to use s3 resource is similar to s3 client from boto3. In the next few blogs, we will learn that s3 resource is easier to use in certain situations.

Conclusion

I hope you have found this useful. In the next few blogs, we will deal with S3 and learn how easy it is to work with S3 using python and AWS CLI. See you in the next blog.

Similar Posts

Leave a Reply

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