
- Updated env.py to correctly handle Python path by adding project root to sys.path - Added proper imports with noqa annotations to handle linting exceptions - Added prepend_sys_path setting to alembic.ini for better path resolution - Fixed import of models to ensure they're properly detected by Alembic
File Upload/Download API
A FastAPI-based REST API for uploading, downloading, and managing files with SQLite database for metadata storage.
Features
- Upload files with size validation
- Download files by ID
- List all uploaded files
- Get detailed file metadata
- Delete files
- Health check endpoint
- OpenAPI documentation
Prerequisites
- Python 3.8+
- pip (Python package manager)
Installation
-
Clone the repository:
git clone <repository-url> cd fileuploaddownloadapi
-
Install dependencies:
pip install -r requirements.txt
-
Run database migrations:
alembic upgrade head
Usage
Running the Server
Start the application with uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
The API will be available at http://localhost:8000
.
API Documentation
Interactive API documentation is available at:
- Swagger UI:
http://localhost:8000/docs
- ReDoc:
http://localhost:8000/redoc
API Endpoints
File Operations
-
Upload a file
POST /api/v1/files/
- Request: Form data with a file
- Response: File metadata with download URL
-
List all files
GET /api/v1/files/
- Response: Array of file metadata objects
-
Get file metadata
GET /api/v1/files/{file_id}
- Response: File metadata object
-
Download a file
GET /api/v1/files/{file_id}/download
- Response: File content as a binary stream
-
Delete a file
DELETE /api/v1/files/{file_id}
- Response: No content (204)
Health Check
- Health check endpoint
GET /health
- Response:
{"status": "healthy"}
Root Endpoint
- API Information
GET /
- Response: Basic API information and links
Configuration
The API uses environment variables for configuration:
Variable | Description | Default Value |
---|---|---|
PROJECT_NAME | Name of the project | "File Upload Download API" |
MAX_FILE_SIZE | Maximum allowed file size in bytes | 10485760 (10MB) |
File Storage
Files are stored in the /app/storage/files
directory. File metadata is stored in a SQLite database at /app/storage/db/db.sqlite
.
Development
Project Structure
fileuploaddownloadapi/
├── app/
│ ├── api/
│ │ └── v1/
│ │ ├── api.py
│ │ └── endpoints/
│ │ └── files.py
│ ├── core/
│ │ └── config.py
│ ├── db/
│ │ ├── base.py
│ │ ├── session.py
│ │ └── migrations/
│ │ └── versions/
│ │ └── 001_create_files_table.py
│ ├── models/
│ │ └── file.py
│ ├── schemas/
│ │ └── file.py
│ └── services/
│ └── file_service.py
├── storage/
│ ├── db/
│ └── files/
├── alembic.ini
├── main.py
└── requirements.txt
Running Migrations
# Create a new migration
alembic revision -m "description"
# Apply all migrations
alembic upgrade head
# Rollback a migration
alembic downgrade -1
# Check current migration version
alembic current
License
Description
Languages
Python
97.7%
Mako
2.3%