How to use Boto3 for AWS Services

blur business close up code
Reading Time: 3 minutes

Hello folks, In this blog, we’re gonna learn how to use Boto3 for AWS Services and how to create/delete s3 bucket. how boto3 and s3 work future we’ll do code using python.

What is boto3?

Boto is a software development kit (SDK) designed to enhance the use of the Python programming language by Amazon Web Services. It is now the official AWS SDK for Python. Boto versions include Boto, Boto3, and Botocore.

Boto3 is the latest version of the SDK and provides support for Python versions 2.6.5, 2.7, and 3.3. It includes some service-specific features to facilitate development.

Prerequisites:

  • Create IAM User
  • Install AWS CLI and configure
  • python3
  • boto3

Install boto3:

install Boto3, by running the command:

pip3 install boto3

Create S3 Bucket Example using resource:

import boto3 
s3 = boto3.resource('s3') 
bucket = s3.create_bucket(Bucket='example-boto3-1235567qwerccse444',    CreateBucketConfiguration={'LocationConstraint': 'ap-south-1'})
print (bucket)

it’ll import boto3 and the resource service is s3 bucket, it’ll create an s3 bucket named as example-boto3-1235567qwerccse444 in the ‘ap-south-1’ region. save the code and name as ‘create-s3.py’

python3 create-s3.py

List S3 Bucket Example using client:

import boto3 
client = boto3.client('s3')
list_bucket=client.list_buckets() 
for bucket in list_bucket['Buckets']:    
   print(bucket['Name'])

The above code will print the list of s3 buckets so save the code and name as ‘list-s3.py’

File Upload on S3 Bucket Example using Resource:

import boto3 
s3 = boto3.resource('s3') 
bucket_name="example-boto3-1235567qwerccse444" 

fileupload= s3.Bucket(bucket_name).upload_file('README.md', 'README.md')

print(fileupload)
python3 upload-file.py

As you can see ‘README.md ‘ file uploaded to the s3 bucket. now we’ll see how to delete the file also.

Delete S3 Bucket Example using client:

import boto3  
client = boto3.client('s3')
bucket_name="example-boto3-1235567qwerccse444" 
response = client.delete_bucket(Bucket=bucket_name) 
python3 delete-s3.py
 how to use Boto3 for AWS Services

so bucket needs to be empty and how can we delete the README.md file by updating the ‘upload-file.py’

 how to use Boto3 for AWS Services

After uploading the file, it’ll also be delete.

python3 upload-file.py
 how to use Boto3 for AWS Services

Alright now our s3 bucket is empty and we’ll be able to delete the s3 bucket.

python3 delete-s3.py
 how to use Boto3 for AWS Services

you can list the s3 bucket to verify if it’s delete or not as shown in the image or in your AWS management console also.

python3 list-s3.py
 how to use Boto3 for AWS Services

Conclusion:

In this blog, we’ve seen how we can create, list, and delete the s3 bucket and upload the files in it by using boto3 in aws. for more information check it.

Written by 

A curious DevOps Intern , love to learn and working on technical skills/tools, know the basics of Linux, Docker, Ansible , Kubernetes and more..

1 thought on “ How to use Boto3 for AWS Services4 min read

Comments are closed.