A reusable file uploader package for FastAPI and AWS S3
Project description
s3_uploader
A reusable, configurable file uploader package for FastAPI that supports uploading single and multiple files to AWS S3 with validations, dynamic folder structure, and file deletion.
Features
- Upload single or multiple files in the same API
- File extension and size validation (configurable)
- Unique filename generation to avoid collisions
- Dynamic folder structure support
- Delete files from S3 by key
- Async and optimized for FastAPI with aioboto3
- Configuration via environment variables or
.envfile using Pydantic
Installation
Install via pip:
pip install s3_uploader
Or install from source:
pip install -e ./s3_uploader
Configuration
The package uses a config.py with Pydantic BaseSettings to load AWS and upload settings from environment variables or a .env file.
Create a .env file in your project root with the following variables:
AWS_ACCESS_KEY_ID=your_aws_access_key
AWS_SECRET_ACCESS_KEY=your_aws_secret_key
AWS_REGION=us-east-1
BUCKET_NAME=your_s3_bucket_name
ALLOWED_EXTENSIONS=jpg,png,pdf,docx
MAX_FILE_SIZE_MB=10
You can customize allowed file extensions and max file size as needed.
Usage Example
Upload single or multiple files in one API endpoint
from fastapi import FastAPI, UploadFile, File, Form
from typing import List, Optional
from s3_uploader import upload_dynamic, delete_file_from_s3
app = FastAPI()
@app.post("/upload/")
async def upload_files(
file: Optional[UploadFile] = File(None),
files: Optional[List[UploadFile]] = File(None),
folder: str = Form("uploads")
):
urls = await upload_dynamic(file=file, files=files, folder=folder)
return {"uploaded_urls": urls}
Delete a file from S3 by key
@app.delete("/delete/")
async def delete_file(key: str):
await delete_file_from_s3(key)
return {"message": f"File {key} deleted successfully"}
Example .env.example
AWS_ACCESS_KEY_ID=your_aws_access_key
AWS_SECRET_ACCESS_KEY=your_aws_secret_key
AWS_REGION=us-east-1
BUCKET_NAME=your_s3_bucket_name
ALLOWED_EXTENSIONS=jpg,png,pdf,docx
MAX_FILE_SIZE_MB=10
Copy this file to .env and update the values.
Requirements
- Python 3.8+
- FastAPI
- aioboto3
- pydantic
License
MIT License
Contributing
Feel free to open issues or submit pull requests to improve the package.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_async_s3uploader-0.1.1.tar.gz.
File metadata
- Download URL: fastapi_async_s3uploader-0.1.1.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7281b8f8dc084be9386e5be0660af3a3d0a2eb700d132cfdeaf86b77891bf4c2
|
|
| MD5 |
22c43a52cd562b128a58e77e87681acd
|
|
| BLAKE2b-256 |
046c4c845adb1be9c583846e731ca28aee4e42d67dc63ee87582bd0029fdf4ab
|
File details
Details for the file fastapi_async_s3uploader-0.1.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_async_s3uploader-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0842317a1628e38cc9ee7eadb68f5a1e6fadebc7e24d99148f00db77e7841c8
|
|
| MD5 |
d81780decd2c8b931dbd1b07490fae60
|
|
| BLAKE2b-256 |
0efa46bf2d242e1a0529e2dba4ddf3b29d96e6710442b1f693f9a26f090b4a87
|