AWS S3 + Python Flask - File Upload, Download, List, and Delete Example
To implement file upload, list, download, and delete operations for AWS S3 in a Flask app, you can follow the steps below.
This example will:
- Upload files to an S3 bucket.
- List files available in the S3 bucket.
- Download files from the S3 bucket.
- Delete files from the S3 bucket.
Step 1: Install Required Libraries
Make sure you have Flask and boto3 installed.
pip install Flask boto3
Step 2: AWS S3 Configuration
You will need AWS credentials and an S3 bucket. You can either:
- Set the AWS credentials as environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY).
- Or use the default AWS credentials file located at ~/.aws/credentials.
Step 3: Flask Application with File Upload, List, Download, and Delete
Here's a simple Flask app that integrates with AWS S3 for file upload, listing, downloading, and deleting.
from flask import Flask, request, send_file, jsonify import boto3 from botocore.exceptions import NoCredentialsError import os # Initialize Flask app app = Flask(__name__) # AWS S3 Configuration AWS_BUCKET_NAME = "your-bucket-name" AWS_ACCESS_KEY_ID = "your-access-key-id" AWS_SECRET_ACCESS_KEY = "your-secret-access-key" AWS_REGION = "your-region" # Initialize S3 Client 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 ) # Route to upload a file to S3 @app.route("/upload", methods=["POST"]) def upload_file(): if "file" not in request.files: return "No file part", 400 file = request.files["file"] if file.filename == "": return "No selected file", 400 try: s3_client.upload_fileobj(file, AWS_BUCKET_NAME, file.filename) return f"File uploaded successfully: {file.filename}", 200 except NoCredentialsError: return "Credentials not available", 403 except Exception as e: return f"An error occurred: {str(e)}", 500 # Route to list files in the S3 bucket @app.route("/list", methods=["GET"]) def list_files(): try: response = s3_client.list_objects_v2(Bucket=AWS_BUCKET_NAME) if 'Contents' in response: file_names = [obj['Key'] for obj in response['Contents']] return jsonify({"files": file_names}), 200 else: return jsonify({"message": "No files found"}), 404 except Exception as e: return jsonify({"error": str(e)}), 500 # Route to download a file from S3 @app.route("/download/<filename>", methods=["GET"]) def download_file(filename): try: file_obj = s3_client.get_object(Bucket=AWS_BUCKET_NAME, Key=filename) return send_file( file_obj['Body'], as_attachment=True, download_name=filename ) except Exception as e: return jsonify({"error": str(e)}), 500 # Route to delete a file from S3 @app.route("/delete/<filename>", methods=["DELETE"]) def delete_file(filename): try: s3_client.delete_object(Bucket=AWS_BUCKET_NAME, Key=filename) return jsonify({"message": f"File {filename} deleted successfully"}), 200 except Exception as e: return jsonify({"error": str(e)}), 500 # Run the Flask app if __name__ == "__main__": app.run(debug=True)
Explanation of Routes:
1. Upload a File (/upload):
- Accepts a file upload using a POST request with the form field file.
- Uses s3_client.upload_fileobj() to upload the file to your S3 bucket.
- Returns a success message or error if it fails.
2. List Files (/list):
- Lists all files in the S3 bucket using s3_client.list_objects_v2().
- Returns a JSON response with the filenames in the bucket.
3. Download a File (/download/<filename>):
- Downloads a file from the S3 bucket.
- Uses send_file() to send the file to the client for download.
4. Delete a File (/delete/<filename>):
- Deletes a file from the S3 bucket using s3_client.delete_object().
- Returns a success message or an error.
Step 4: Running the Flask App
1. Start the Flask application:
python app.py
2. To test the API:
- Upload a file (e.g., using curl):
curl -X POST -F "file=@path_to_your_file" http://localhost:5000/upload
- List files:
curl http://localhost:5000/list
- Download a file:
curl -O http://localhost:5000/download/your-file-name
- Delete a file:
curl -X DELETE http://localhost:5000/delete/your-file-name
Final Notes:
- Security: Make sure you don't hard-code your AWS credentials directly in the code. You can use environment variables, the AWS credentials file (~/.aws/credentials), or IAM roles (if deploying to AWS services like EC2).
- File Size Consideration: If you're uploading large files, consider using multipart upload for efficiency.
- Error Handling: The code catches general exceptions and provides error messages to the client, which can be expanded for more specific error types.
This should give you a full setup for working with AWS S3 in a Flask app for file upload, list, download, and delete operations. Let me know if you need further details!