Automate File Uploads to AWS S3 Using Python and Boto3

AWS S3 is a widely used cloud storage service, and with Python’s Boto3 library, you can easily upload files programmatically. In this post, you will learn how to set up Boto3 and upload files to S3 using a simple Python script.

Step 1: Install Boto3

pip install boto3

Step 2: Configure AWS Credentials

Set up your credentials using the AWS CLI or by saving them in a file named ~/.aws/credentials.

Step 3: Python Script to Upload File

import boto3

s3 = boto3.client("s3")

s3.upload_file("example.txt", "your-bucket-name", "example.txt")

Step 4: Add Error Handling

try:
    s3.upload_file("example.txt", "your-bucket-name", "example.txt")
    print("Upload successful")
except Exception as e:
    print("Upload failed:", e)

This script is ideal for automating backups, data logging, or user uploads in your apps.

Bonus Tips

  • Use os.listdir() to upload multiple files
  • Use boto3.resource if you prefer object-oriented syntax

With Boto3, automating S3 uploads becomes a quick and reliable task for any Python developer.

Comments

Popular posts from this blog

Docker for Beginners: Build, Ship, and Run Apps with Ease

Top 7 Real-World Projects to Learn React, Python, and AWS (Beginner to Advanced)