File Upload Web Application Example
This example demonstrates how to use file-keeper in a web application with Flask.
Overview
This Flask application shows:
- File uploads using file-keeper
- Security best practices
- Error handling
- Different storage backends (filesystem, memory)
Prerequisites
- Python 3.10+
- pip
Installation
- Install dependencies:
pip install -r requirements.txt
Usage
Run with Filesystem Storage (Default)
python app.py
Run with Memory Storage (for testing)
STORAGE_TYPE=memory python app.py
Run with Custom Storage Path
STORAGE_PATH=/path/to/uploads python app.py
API Endpoints
POST /upload- Upload a fileGET /files- List all filesGET /file/<filename>- Download a fileDELETE /file/<filename>- Delete a fileGET /health- Health check
Example Usage
Upload a file:
curl -X POST -F "file=@myfile.txt" http://localhost:5000/upload
List files:
curl http://localhost:5000/files
Download a file:
curl http://localhost:5000/file/myfile.txt -O
Security Features
- Filename sanitization using werkzeug's secure_filename
- File type validation
- File size limits
- Directory traversal prevention
- Capability checking before operations
Configuration
You can configure the application using environment variables:
STORAGE_TYPE: Storage backend ('fs' for filesystem, 'memory' for in-memory)STORAGE_PATH: Path for filesystem storage (defaults to './uploads')
Testing
The application includes basic functionality testing. For comprehensive testing, consider using pytest with the following test structure:
# test_app.py
import pytest
from app import app
@pytest.fixture
def client():
app.config['TESTING'] = True
with app.test_client() as client:
yield client
def test_health(client):
rv = client.get('/health')
assert b'healthy' in rv.data