DEV Community

Cover image for Boto3 -Basics and S3 samples
Srinivasulu Paranduru for AWS Community Builders

Posted on • Updated on

Boto3 -Basics and S3 samples

Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python, which allows Python developers to write software that makes use of services like Amazon S3 and Amazon EC2. You can find the latest, most up to date, documentation at our doc site, including a list of services that are supported

Install Boto3
Install the latest Boto3 release via pip:

pip install boto3
Enter fullscreen mode Exit fullscreen mode

Configuration
Before you are using Boto3, you need to setup authentication credentials for your AWS account using either the IAM Console or the AWS CLI.
Make sure you are having IAM User with the access key and secret access key then use the following to configure the credentials

aws configure
Enter fullscreen mode Exit fullscreen mode

By default, the credentials are stored in the location ~/.aws/credentials

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY
region=eu-west-1
Enter fullscreen mode Exit fullscreen mode

Using Boto3

To use Boto3, you must first import it and indicate which service or services you’re going to use:

import boto3

# Let's use Amazon S3
s3 = boto3.resource('s3')
Enter fullscreen mode Exit fullscreen mode

1.Create AWS S3 bucket

##File name : s3_create.py

import boto3
s3_client = boto3.client('s3')
## Create bucket
response = s3_client.create_bucket(
    Bucket='srini-letterkenny-newyear-2024',
    CreateBucketConfiguration={
        'LocationConstraint': 'eu-west-1'
    }
)
print(response)

Enter fullscreen mode Exit fullscreen mode

Response of create AWS S3 bucket

PS C:\Desktop>python3 s3_create.py
{'ResponseMetadata': {'RequestId': '58PB5DVWCMRF82J6', 'HostId': 'Gia0WMrN5EjiYUGPfVh4xszSTdJYrgRcEY4P4GW+nFJdvs7GVPL7m8FNwFtmnCogIvBXN9AU6Zs=', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amz-id-2': 'Gia0WMrN5EjiYUGPfVh4xszSTdJYrgRcEY4P4GW+nFJdvs7GVPL7m8FNwFtmnCogIvBXN9AU6Zs=', 'x-amz-request-id': '58PB5DVWCMRF82J6', 'date': 'Mon, 25 Dec 2023 14:30:41 GMT', 'location': 'http://srini-letterkenny-newyear-2024.s3.amazonaws.com/', 'server': 'AmazonS3', 'content-length': '0'}, 'RetryAttempts': 0}, 'Location': 'http://srini-letterkenny-newyear-2024.s3.amazonaws.com/'}
Enter fullscreen mode Exit fullscreen mode

2.List AWS S3 buckets

##File name : s3_list.py
import boto3
s3_client = boto3.client('s3')

# List the buckets
for bucket in s3_client.list_buckets()["Buckets"]:
    print(bucket['Name'])
Enter fullscreen mode Exit fullscreen mode

Response of List AWS S3 bucket

PS C:\Desktop> python3 s3_list.py
srini-letterkenny-christmas-2023
Enter fullscreen mode Exit fullscreen mode

3.Delete AWS S3 bucket

  • All objects (including all object versions and delete markers) in the bucket must be deleted before the bucket itself can be deleted.
  • You must have the s3:DeleteBucket permission on the specified bucket in a policy.
##File name : s3_delete.py

import boto3
s3_client = boto3.client('s3')

response = s3_client.delete_bucket(
    Bucket='srini-letterkenny-newyear-2024',
)

print(response)
Enter fullscreen mode Exit fullscreen mode
PS C:\Desktop> python3 s3_delete.py
{'ResponseMetadata': {'RequestId': 'K7FP3Y1BR7N6BE01', 'HostId': 'EdI1Uzn4xM7WpJCoFnOnPJDqqAMmL51QBedTg6RRmwSzKVy+Vr1QXQYbyT+xPDSgacAe9HLzZJU=', 'HTTPStatusCode': 204, 'HTTPHeaders': {'x-amz-id-2': 'EdI1Uzn4xM7WpJCoFnOnPJDqqAMmL51QBedTg6RRmwSzKVy+Vr1QXQYbyT+xPDSgacAe9HLzZJU=', 'x-amz-request-id': 'K7FP3Y1BR7N6BE01', 'date': 'Mon, 25 Dec 2023 14:29:02 GMT', 'server': 'AmazonS3'}, 'RetryAttempts': 0}}
Enter fullscreen mode Exit fullscreen mode

4.Download files from S3 bucket

##File Name: s3_download.py

import boto3
s3_client = boto3.client('s3')
bucket_name = "srini-letterkenny-christmas-2023"
file_name   = "automation.docx"
download_path = "C:\\Terraform\\automation.docx"
s3_client.download_file(bucket_name, file_name, download_path)

Enter fullscreen mode Exit fullscreen mode
PS C:\Desktop> python3 s3_download.py
Enter fullscreen mode Exit fullscreen mode

Note: Change the parameters based on the aws s3 bucket, filename and download path.

bucket_name = Name of the bucket from where you wanted to download the files
file_name = Name of the file, you wanted to download
download_path = local path of the system where you wanted to download the files from S3 bucket.

5. Upload files to S3 bucket (upload_file)
Upload a file to an S3 object

##File Name: s3_upload.py
import boto3
s3_client = boto3.client('s3')
bucket_name = "srini-letterkenny-christmas-2023"
file_name   = "C:\\Terraform\\Cert.pdf"
upload_file= "PlatinumCert.pdf"
s3_client.upload_file(file_name, bucket_name, upload_file)
Enter fullscreen mode Exit fullscreen mode
PS C:\Desktop> python3 s3_upload.py
Enter fullscreen mode Exit fullscreen mode

File uploaded in S3 bucket
Image description

References: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html

Conclusion : Discussed about AWS Boto3 SDK and written some programs using Python 3 + Boto3

If you like my blogs, please like and any suggestion give comments and follow me in dev.to and linked in https://www.linkedin.com/in/srinivasuluparanduru/

Top comments (0)