
- Set up FastAPI project structure - Implement database models and migrations for file metadata - Create file upload endpoint with size validation - Implement file download and listing functionality - Add health check and API information endpoints - Create comprehensive documentation
158 lines
3.4 KiB
Markdown
158 lines
3.4 KiB
Markdown
# 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
|
|
|
|
1. Clone the repository:
|
|
```bash
|
|
git clone <repository-url>
|
|
cd fileuploaddownloadapi
|
|
```
|
|
|
|
2. Install dependencies:
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
3. Run database migrations:
|
|
```bash
|
|
alembic upgrade head
|
|
```
|
|
|
|
## Usage
|
|
|
|
### Running the Server
|
|
|
|
Start the application with uvicorn:
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
# 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
|
|
|
|
[MIT License](LICENSE) |