Enterprise-grade file upload, storage, and processing microservice built with Go.
The PyAirtable File Service is a high-performance, scalable microservice designed for handling file operations in multi-tenant environments. It provides secure file upload, storage, processing, and retrieval capabilities with support for multiple storage backends and advanced file processing features.
- Multi-format Support: Images, documents, videos, and any file type
- Multiple Storage Backends: Local filesystem, Amazon S3, Google Cloud Storage
- File Deduplication: Hash-based duplicate detection to save storage
- Secure Upload: Presigned URLs for direct client uploads
- Chunked Upload: Support for large file uploads with resumable sessions
- File Validation: MIME type checking, size limits, and virus scanning
- Image Processing: Resize, crop, format conversion, thumbnail generation
- Document Processing: PDF preview generation, metadata extraction
- Compression: Automatic file optimization and compression
- Background Jobs: Asynchronous processing with job queues
- Metadata Extraction: Automatic EXIF, document, and media metadata extraction
- Tenant Isolation: Multi-tenant architecture with data segregation
- Access Control: Fine-grained permissions and public/private files
- Secure URLs: Time-limited, signed URLs for file access
- Virus Scanning: Integrated malware detection
- Audit Logging: Comprehensive access and operation logging
- Streaming: Memory-efficient streaming for large files
- CDN Ready: Optimized for CDN integration
- Caching: Redis-based caching for frequently accessed data
- Quota Management: Per-tenant storage and file count limits
- Rate Limiting: Configurable rate limits and throttling
- Go 1.21+
- Docker & Docker Compose
- PostgreSQL 15+
- Redis 7+
-
Clone and setup:
git clone <repository-url> cd fileservice cp .env.example .env
-
Start dependencies:
make dev
-
Run the service:
make run
The service will be available at http://localhost:8080
# Build and run with Docker Compose
make docker-compose-up
# Or build and run individually
make docker-build
make docker-run
Method | Endpoint | Description | Auth Required |
---|---|---|---|
POST | /files/upload |
Upload file (multipart) | Yes |
POST | /files/upload-url |
Generate presigned upload URL | Yes |
GET | /files/:id |
Get file metadata | Yes |
GET | /files/:id/download |
Download file or get download URL | Yes |
DELETE | /files/:id |
Delete file | Yes |
POST | /files/:id/process |
Process file (resize, convert) | Yes |
GET | /files |
List files (tenant-scoped) | Yes |
GET | /files/:id/thumbnail |
Get thumbnail | Yes |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
POST | /files/upload/init |
Initiate chunked upload | Yes |
GET | /files/upload/:sessionId/status |
Get upload status | Yes |
POST | /files/upload/:sessionId/complete |
Complete chunked upload | Yes |
DELETE | /files/upload/:sessionId |
Abort chunked upload | Yes |
POST | /files/upload/chunk/:sessionId |
Upload chunk | Yes |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
GET | /files/:id/jobs |
Get processing jobs for file | Yes |
GET | /files/stats/tenant/:tenantId |
Get file statistics | Yes |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
GET | /quota/:tenantId |
Get tenant quota | Yes |
PUT | /quota/:tenantId |
Set tenant quota | Yes |
GET | /quota/exceeded |
Get quota exceeded tenants | Yes |
Method | Endpoint | Description | Auth Required |
---|---|---|---|
GET | /health |
Health check | No |
GET | /metrics |
Prometheus metrics | No |
Protected endpoints require a valid JWT token in the Authorization header:
curl -H "Authorization: Bearer <jwt-token>" \
http://localhost:8080/api/v1/protected
Configuration is managed through environment variables:
Variable | Description | Default |
---|---|---|
PORT |
Server port | 8080 |
DB_HOST |
Database host | localhost |
DB_PORT |
Database port | 5432 |
DB_NAME |
Database name | file_service_db |
REDIS_HOST |
Redis host | localhost |
REDIS_PORT |
Redis port | 6379 |
JWT_SECRET |
JWT signing secret | your-secret-key |
LOG_LEVEL |
Logging level | info |
See .env.example
for a complete list.
make help # Show all available commands
make deps # Download dependencies
make build # Build the application
make run # Run the application
make test # Run tests
make test-coverage # Run tests with coverage
make bench # Run benchmarks
make lint # Run linter
make format # Format code
make clean # Clean build artifacts
fileservice/
├── cmd/file-service/ # Application entrypoint
├── internal/ # Private application code
│ ├── config/ # Configuration management
│ ├── handlers/ # HTTP handlers
│ ├── services/ # Business logic
│ ├── repositories/ # Data access layer
│ ├── middleware/ # Custom middleware
│ └── models/ # Data models
├── pkg/ # Public packages
│ ├── database/ # Database utilities
│ ├── redis/ # Redis utilities
│ ├── logger/ # Logging utilities
│ └── metrics/ # Metrics utilities
├── test/ # Test files
├── deployments/ # Deployment configs
│ ├── docker/ # Docker configs
│ └── k8s/ # Kubernetes manifests
└── .github/workflows/ # CI/CD workflows
# Run all tests
make test
# Run with coverage
make test-coverage
# Run only unit tests
go test ./test/unit/...
# Run only integration tests
go test ./test/integration/...
# Run benchmarks
make bench
- Add models in
internal/models/
- Create repositories in
internal/repositories/
- Implement services in
internal/services/
- Add handlers in
internal/handlers/
- Register routes in
cmd/file-service/main.go
- Add tests in
test/unit/
andtest/integration/
# Build image
make docker-build
# Run container
make docker-run
# Deploy to Kubernetes
kubectl apply -k deployments/k8s/
# Check deployment status
kubectl get pods -n fileservice
The service includes Kustomize configurations for different environments:
- Development: Basic setup with minimal resources
- Staging: Production-like setup with monitoring
- Production: Full setup with HPA, ingress, and security policies
- Liveness:
GET /health
- Readiness:
GET /health
(customize as needed)
Prometheus metrics are available at /metrics
:
- HTTP request metrics (count, duration)
- Database connection metrics
- Redis connection metrics
- Custom business metrics
Structured JSON logging with configurable levels:
{
"time": "2024-01-01T12:00:00Z",
"level": "INFO",
"msg": "Server started",
"port": "8080"
}
- JWT Authentication: Secure token-based auth
- CORS: Configurable cross-origin policies
- Rate Limiting: Built-in request rate limiting
- Security Headers: Helmet middleware for security headers
- Input Validation: Request validation and sanitization
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature
- Commit changes:
git commit -m 'Add amazing feature'
- Push to branch:
git push origin feature/amazing-feature
- Open a Pull Request
- Follow Go best practices and conventions
- Write tests for new features
- Update documentation as needed
- Run linter and formatter before submitting
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Author: PyAirtable Team
- Issues: GitHub Issues
- Documentation: Wiki
Generated with Go Microservice Template