flask upload file to s3

from flask import Flask, request
import boto3
from botocore.exceptions import NoCredentialsError

app = Flask(__name__)

# Step 1: Configure AWS S3 credentials
AWS_ACCESS_KEY = 'your_access_key'
AWS_SECRET_KEY = 'your_secret_key'
AWS_BUCKET_NAME = 'your_bucket_name'

# Step 2: Initialize the S3 client
s3 = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_KEY)

# Step 3: Define a route to handle file upload
@app.route('/upload', methods=['POST'])
def upload_file():
    # Step 4: Get the uploaded file from the request
    uploaded_file = request.files['file']

    # Step 5: Check if the file exists
    if uploaded_file:
            # Step 6: Upload the file to S3
            s3.upload_fileobj(uploaded_file, AWS_BUCKET_NAME, uploaded_file.filename)

            # Step 7: Provide a success message
            return "File uploaded successfully to S3."

        except NoCredentialsError:
            # Step 8: Handle the case where AWS credentials are missing
            return "AWS credentials not available."

        # Step 9: Handle the case where no file is provided
        return "No file provided."

if __name__ == '__main__':
    # Step 10: Run the Flask application