Using Amazon S3 for File Storage in Python Web Apps
Amazon S3 (Simple Storage Service) is a scalable, durable, and secure cloud storage solution. Using S3 in a Python web application allows you to store files, images, and static content off your server, reducing storage and improving scalability.
This guide covers how to integrate S3 with Python web apps (Flask/Django).
1. Prerequisites
AWS Account
IAM User with permissions:
s3:PutObject, s3:GetObject, s3:ListBucket
Python Packages:
pip install boto3 Flask python-dotenv
2. Configure AWS Credentials
Use environment variables or .env file:
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_REGION=us-east-1
S3_BUCKET_NAME=my-bucket
Load them in Python using python-dotenv:
import os
from dotenv import load_dotenv
load_dotenv()
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")
AWS_REGION = os.getenv("AWS_REGION")
S3_BUCKET_NAME = os.getenv("S3_BUCKET_NAME")
3. Initialize Boto3 S3 Client
import boto3
s3_client = boto3.client(
"s3",
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_REGION
)
4. Uploading Files to S3
def upload_file_to_s3(file, bucket_name, object_name=None):
if object_name is None:
object_name = file.filename
try:
s3_client.upload_fileobj(file, bucket_name, object_name)
return f"https://{bucket_name}.s3.{AWS_REGION}.amazonaws.com/{object_name}"
except Exception as e:
print("Upload Error:", e)
return None
Example: Flask File Upload Endpoint
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/upload', methods=['POST'])
def upload():
if 'file' not in request.files:
return jsonify({"error": "No file uploaded"}), 400
file = request.files['file']
file_url = upload_file_to_s3(file, S3_BUCKET_NAME)
if file_url:
return jsonify({"file_url": file_url}), 200
else:
return jsonify({"error": "Upload failed"}), 500
if __name__ == '__main__':
app.run(debug=True)
5. Downloading / Accessing Files
Public Access URL:
file_url = f"https://{S3_BUCKET_NAME}.s3.{AWS_REGION}.amazonaws.com/{object_name}"
Generate Temporary Signed URL (Private Objects):
def generate_presigned_url(object_name, expiration=3600):
return s3_client.generate_presigned_url(
'get_object',
Params={'Bucket': S3_BUCKET_NAME, 'Key': object_name},
ExpiresIn=expiration
)
6. Deleting Files from S3
def delete_file_from_s3(object_name):
try:
s3_client.delete_object(Bucket=S3_BUCKET_NAME, Key=object_name)
return True
except Exception as e:
print("Delete Error:", e)
return False
7. Security Best Practices
Do not hardcode credentials in code
Use IAM roles if running on EC2 / Lambda / ECS
Enable bucket policies and private ACLs
Use server-side encryption (SSE) for sensitive files
Consider presigned URLs for temporary secure access
8. Benefits of Using S3
Virtually unlimited storage
High durability (99.999999999%)
Scalable and low maintenance
Supports static website hosting
Integrates easily with CDNs like CloudFront
9. Tips for Python Web Apps
Stream uploads directly to S3 instead of saving locally
Use multipart uploads for large files (>100 MB)
Cache S3 URLs or use CloudFront for performance
Handle exceptions like ClientError for better reliability
10. Conclusion
Integrating Amazon S3 with Python web apps is straightforward using Boto3. By offloading file storage to S3, your app becomes scalable, secure, and cloud-ready. This approach works for user uploads, media files, backups, and static content delivery.
Learn Fullstack Python Training in Hyderabad
Read More
Deployment and Scaling in Python
Optimizing Communication Between React and Django
Full Stack Python with Django Channels for WebSocket Support
State Management in Full Stack Python Projects with React
At Our Quality Thought Training Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments