No nonsense guide to automating AWS S3 with Python (Free Tier-Friendly!) - Part 1

Saurab DahalSaurab Dahal
2 min read

Clicking through the AWS console just to create some buckets ?

Nah !!!! . Let’s automate it with Python! Here’s how to work with buckets using boto3—with zero cost on AWS Free Tier. Please go through the free tier guide to understand more about how free tier works.

Why automate when you can just use AWS console ?

Great question !!!!!

Here’s why …

  1. No unnecessary clicks and page reloads

  2. All necessary configuration at your command

  3. No overwhelming options

Before we dive into code we need to configure the environment. Let's go through some initial steps first.

Prerequisites

  • We need to create a new IAM user. NEVER USE YOUR ROOR user to access your AWS services. To create a new user following the best practices follow this guide

  • Python 3.8+, virtualenv, and AWS CLI installed.

  • run aws configure to connect your local development with your aws account

Create buckets

Now that everything is setup and working, we are ready to move to the project itself. Create a file BucketManager.py or any name you like and create class. I like to write with OOP you can use functional style. Choosing a bucket name is important. There are certain rules we must follow while choosing a bucket name. Please follow this official guide for more comprehensive detail

To create a bucket we use create_bucket() function.

import boto3


class BucketManager:
    def __init__(self):
        # please keep in mind following things while choosing a bucket name
        # 1. Bucket names must be 3 to 63 characters and unique within the global namespace
        # 2. Bucket names must also begin and end with a letter or number and must be lowercase
        # 3. Valid characters are a-z, 0-9, periods (.), and hyphens (-)

        self.bucket_name = "aws-demo-fruit" 
        self.s3 = boto3.client('s3')

        def create_bucket(self, region, bucket_name=None):
        try:
            if not bucket_name:
                bucket_name = self.bucket_name
            self.s3.create_bucket(Bucket=bucket_name, CreateBucketConfiguration={'LocationConstraint': region})
        except ClientError as e:
            print(f"Error: {e.response['Error']['Message']}")

Run this code from main file

from s3.Bucket import BucketManager

if __name__ == '__main__':
    Bucket().create_bucket(region="us-east-2")

List Buckets

In order to list all the buckets in you account we use list_bucket() function.

    def list_all_buckets(self):
        try:
            resp = self.s3.list_buckets()
            buckets = resp.get('Buckets', [])
            if buckets:
                for b in buckets:
                    print(b)
            else:
                print("no buckets found")
        except Exception as e:
            print(f"Error: {e.args}")

Run the code from your main file

from s3.Bucket import BucketManager

if __name__ == '__main__':
    Bucket().list_all_buckets()

You should see response similar to this

{'Name': 'aws-demo-fruit', 'CreationDate': datetime.datetime(2025, 7, 4, 15, 29, 52, tzinfo=tzutc())}

In the next guide, we will learn how to upload files in s3 .. stay tuned.

Full code on my GitHub .

0
Subscribe to my newsletter

Read articles from Saurab Dahal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saurab Dahal
Saurab Dahal