How to create an S3 Bucket using Python Boto3 on AWS
In this article, we will learn to create an S3 bucket using the Python Boto3 library. We will also see the steps to delete the bucket we created. We will use the "create_bucket" & "delete_bucket" methods to create and delete a bucket respectively.
Before we proceed I assume that you are familiar with S3 bucket, but if you are not familiar with the S3 bucket service, click here to learn to create a bucket from AWS Console.
Pre-requisites
- AWS Account (Create if you don’t have one)
- Basic understanding of S3
- Basic understanding of Python
- Python available on the system
What we will do
- Install Boto3
- Know the required methods.
- Create and delete an S3 bucket.
Install Boto3 dependencies
Python comes by default in Ubuntu Server, so you do not need to install it.
To check the Python version on your system, use the following command.
which python
/usr/bin/python --version
OR
python --version
If you do not have pip and you are using Ubuntu, execute the following command to first update the local repo.
sudo apt update
To install pip, use the following command.
sudo apt install python-pip
To check the version of Pip installed, execute the following command.
pip --version
Once you have python and pip, you can install Boto3.
Installing Boto3 is very simple and straight. To install Boto3 use the following command.
pip install boto3
To check if the Boto3 is installed and its version, execute the following command.
pip show boto3
Know the required methods
To create a bucket, we will use the "create_bucket" method. Following is the syntax of the method with all the parameters which it can accept. We shall not use all these parameters to create a Bucket but you can give it a try if you want.
Request Syntax of create_bucket method
response = client.create_bucket( ACL='private'|'public-read'|'public-read-write'|'authenticated-read', Bucket='string', CreateBucketConfiguration={ 'LocationConstraint': 'EU'|'eu-west-1'|'us-west-1'|'us-west-2'|'ap-south-1'|'ap-southeast-1'|'ap-southeast-2'|'ap-northeast-1'|'sa-east-1'|'cn-north-1'|'eu-central-1' }, GrantFullControl='string', GrantRead='string', GrantReadACP='string', GrantWrite='string', GrantWriteACP='string', ObjectLockEnabledForBucket=True|False )
- ACL: The canned ACL to apply to the bucket.
- Bucket: The name of the bucket to create. This must be unique globally and no 2 buckets can have the same name.
- CreateBucketConfiguration: The configuration information for the bucket.
LocationConstraint: Specifies the Region where you want to create a bucket. US East (N. Virginia) Region (us-east-1) is the default region and buckets get created here if the region is not specified.. - GrantFullControl: Allows grantee the read, write, read ACP, and write ACP permissions on the bucket.
- GrantRead: Allows grantee to list the objects in the bucket.
- GrantReadACP: Allows grantee to read the bucket ACL.
- GrantWrite: Allows grantee to create, overwrite, and delete any object in the bucket.
- GrantWriteACP: Allows grantee to write the ACL for the applicable bucket.
- ObjectLockEnabledForBucket: Specifies whether you want S3 Object Lock to be enabled for the new bucket.
Following is the method syntax to delete the bucket we created.
Request Syntax of delete_bucket method
response = client.delete_bucket(
Bucket='string'
)
- Bucket: Specifies the bucket being deleted. You need to specify your S3 Bucket's name here which you want to delete.
Create and delete an S3 Bucket
Create "config.properties" file which will contain your AWS User aws_access_key_id_value ,aws_secret_access_key_value and region. Add your keys in this file.
config.properties
aws_access_key_id_value='ACCESS-KEY-OF-THE-AWS-ACCOUNT' aws_secret_access_key_value='SECRETE-KEY-OF-THE-AWS-ACCOUNT' region_name_value='eu-west-3'
Now, create a file "create-s3-bucket.py" and add the following code in it. This code will read the values defined in the previous step and create a bucket with the name you define in this file. Here, I ll create a bucket named "rahul-boto3-test-delete", change this to the one you want.
vim create-s3-bucket.py
import boto3
def getVarFromFile(filename):
import imp
f = open(filename)
global data
data = imp.load_source('data', '', f)
f.close()
getVarFromFile('config.properties')
client = boto3.client(
's3',
aws_access_key_id=data.aws_access_key_id_value,
aws_secret_access_key=data.aws_secret_access_key_value
)
response = client.create_bucket(
Bucket='rahul-boto3-test-delete',
CreateBucketConfiguration={
'LocationConstraint': 'eu-west-3',
},
)
print (response)
Create a file "delete-s3-bucket.py" which will contain the code to delete a bucket. Copy-paste the following code in it.
vim delete-s3-bucket.py
def getVarFromFile(filename):
import imp
f = open(filename)
global data
data = imp.load_source('data', '', f)
f.close()
getVarFromFile('config.properties')
client = boto3.client(
's3',
aws_access_key_id=data.aws_access_key_id_value,
aws_secret_access_key=data.aws_secret_access_key_value
)
response = client.delete_bucket(Bucket='rahul-boto3-test-delete')
print (response)
Now you are ready to create a bucket.
To create a bucket, execute the file "create-s3-bucket.py" using the following command.
python create-s3-bucket.py
If you no longer need the bucket and want to delete it, execute the file "delete-s3-bucket.py" using the following command.
python delete-s3-bucket.py
Conclusion
We saw how easy it is to create an S3 bucket using the Boto3 library of Python and also to delete it. We created a simple bucket, you can customise the code as per your need & requirement and create a bucket using different parameters available in the create_bucket method.