flask Upload file to local s3

  1. Install required packages:
pip install Flask Flask-Uploads boto3
  1. Import necessary modules in your Flask application:
from flask import Flask, request, render_template
from flask_uploads import UploadSet, configure_uploads, IMAGES
import boto3
  1. Configure Flask app and specify upload settings:
app = Flask(__name__)
photos = UploadSet("photos", IMAGES)
app.config["UPLOADED_PHOTOS_DEST"] = "uploads"
configure_uploads(app, photos)
  1. Set up AWS S3 credentials:
s3 = boto3.client(
bucket_name = "your_bucket_name"
  1. Create a route to handle file uploads:
@app.route("/", methods=["GET", "POST"])
def upload_file():
    if request.method == "POST" and "photo" in request.files:
        photo = request.files["photo"]
        s3.upload_fileobj(photo, bucket_name, photo.filename)
        return "File uploaded successfully to S3!"
    return render_template("upload.html")
  1. Create an HTML template for the file upload form (upload.html):
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Upload to S3</title>
    <h1>Upload File to S3</h1>
    <form method="POST" enctype="multipart/form-data">
        <input type="file" name="photo" accept=".jpg, .jpeg, .png">
        <button type="submit">Upload</button>
  1. Run the Flask application:
python your_app_name.py
  1. Open your browser and visit to test the file upload functionality.