MediaGit Storage Backend
Unified cloud storage abstraction layer for MediaGit, providing consistent APIs across multiple storage providers.
Overview
MediaGit Storage provides a trait-based abstraction (StorageBackend) for working with different cloud storage providers through a unified interface. This enables seamless storage provider switching, multi-cloud deployments, and local development with emulators.
Supported Backends
Production Backends
- AWS S3 - Industry-standard object storage with multipart upload support
- Azure Blob Storage - Microsoft Azure cloud storage with chunked uploads
- Google Cloud Storage (GCS) - Google Cloud object storage with resumable uploads
- MinIO - Self-hosted S3-compatible storage for on-premise deployments
- Backblaze B2 - Cost-effective cloud storage with S3-compatible API
- DigitalOcean Spaces - S3-compatible object storage for app platforms
Development Backends
- Local Filesystem - File-based storage for development and testing
- In-Memory Mock - Ephemeral storage for unit tests
- Cache - LRU caching layer for any backend
Quick Start
Add Dependency
[]
= { = "../mediagit-storage" }
= { = "1", = ["full"] }
Basic Usage
use ;
async
Backend Configuration
AWS S3
use S3Backend;
// From environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION)
let s3 = from_env.await?;
// Explicit configuration
let s3 = new.await?;
Azure Blob Storage
use AzureBackend;
// With connection string
let azure = with_connection_string.await?;
// With account key
let azure = with_account_key.await?;
// With SAS token
let azure = with_sas_token.await?;
Google Cloud Storage
use GcsBackend;
// With service account JSON
let gcs = new.await?;
// From environment (GCS_PROJECT_ID, GCS_BUCKET_NAME, GOOGLE_APPLICATION_CREDENTIALS)
let gcs = from_env.await?;
MinIO
use MinIOBackend;
// Self-hosted MinIO
let minio = new.await?;
// From environment (MINIO_ENDPOINT, MINIO_BUCKET, MINIO_ACCESS_KEY, MINIO_SECRET_KEY)
let minio = from_env.await?;
Backblaze B2 / DigitalOcean Spaces
use ;
// Backblaze B2
let b2 = new_b2.await?;
// DigitalOcean Spaces
let spaces = new_spaces.await?;
Local Development
use LocalBackend;
// Local filesystem storage
let local = new.await?;
// In-memory mock for testing
use MockBackend;
let mock = new;
StorageBackend Trait
All backends implement the StorageBackend trait:
Features
Multipart & Chunked Uploads
- S3: Automatic multipart upload for files >100MB
- Azure: 4MB chunk uploads for large blobs
- GCS: 256KB resumable uploads for files >5MB
Retry Logic
- Exponential backoff for transient failures
- Configurable retry attempts (default: 3)
- Automatic retry on network errors
Concurrent Operations
- Thread-safe implementations (Send + Sync)
- Parallel uploads and downloads
- Efficient connection pooling
Error Handling
- Comprehensive error types with context
- Detailed error messages for debugging
- Idempotent delete operations
Testing
Unit Tests (No Dependencies)
# Run all unit tests
# Test specific backend
Integration Tests (Requires Emulators)
# Start all emulators
# Run all integration tests
# Test specific backend
See TESTING.md for complete testing guide.
Test Coverage
- Unit Tests: 62 test cases (configuration, validation, traits)
- Integration Tests: 89 test cases (CRUD, concurrent, edge cases)
- Total: 151 comprehensive tests
Integration Test Breakdown
- S3 LocalStack: 21 tests
- Azure Azurite: 21 tests
- GCS Emulator: 24 tests
- MinIO Docker: 23 tests
Performance
Throughput Benchmarks (Emulators)
- LocalStack (S3): ~50-100 MB/s
- Azurite (Azure): ~80-150 MB/s
- GCS Emulator: ~30-60 MB/s
- MinIO: ~100-200 MB/s
Optimization Features
- Connection pooling and reuse
- Automatic multipart uploads
- Configurable chunk sizes
- Parallel operations support
Architecture
mediagit-storage/
├── src/
│ ├── lib.rs # StorageBackend trait and exports
│ ├── s3.rs # AWS S3 implementation (781 lines)
│ ├── azure.rs # Azure Blob Storage (819 lines)
│ ├── gcs.rs # Google Cloud Storage (894 lines)
│ ├── minio.rs # MinIO S3-compatible (1,111 lines)
│ ├── b2_spaces.rs # B2/Spaces unified (1,267 lines)
│ ├── local.rs # Local filesystem backend
│ ├── mock.rs # In-memory mock backend
│ ├── cache.rs # LRU caching layer
│ └── error.rs # Error types and handling
├── tests/
│ ├── s3_localstack_tests.rs # S3 integration tests
│ ├── azure_azurite_tests.rs # Azure integration tests
│ ├── gcs_emulator_tests.rs # GCS integration tests
│ ├── minio_docker_tests.rs # MinIO integration tests
│ └── gcs_integration_tests.rs # GCS unit tests
├── docker-compose.yml # Emulator orchestration
├── TESTING.md # Complete testing guide
└── README.md # This file
Dependencies
Core Dependencies
async-trait- Async trait definitionstokio- Async runtimeanyhow- Error handling
Backend SDKs
aws-sdk-s3- AWS S3 SDKazure_storage_blobs- Azure SDKgoogle-cloud-storage- GCS SDK
Optional Dependencies
serde- Serialization (configuration)tracing- Logging and instrumentation
Environment Variables
AWS S3
AWS_ACCESS_KEY_ID- AWS access keyAWS_SECRET_ACCESS_KEY- AWS secret keyAWS_REGION- AWS region (e.g., us-east-1)AWS_SESSION_TOKEN- Optional session token
Azure Blob Storage
AZURE_STORAGE_ACCOUNT- Storage account nameAZURE_STORAGE_KEY- Account keyAZURE_STORAGE_CONNECTION_STRING- Full connection stringAZURE_STORAGE_SAS_TOKEN- SAS token
Google Cloud Storage
GCS_PROJECT_ID- GCP project IDGCS_BUCKET_NAME- Bucket nameGOOGLE_APPLICATION_CREDENTIALS- Path to service account JSON
MinIO
MINIO_ENDPOINT- MinIO endpoint URLMINIO_BUCKET- Bucket nameMINIO_ACCESS_KEY- Access keyMINIO_SECRET_KEY- Secret key
Production Deployment
Security Best Practices
- Never commit credentials - Use environment variables or secret managers
- Enable encryption - Use server-side encryption (S3 SSE, Azure encryption)
- Restrict access - Use IAM policies and minimal permissions
- Rotate credentials - Regular key rotation and auditing
- Enable versioning - Bucket versioning for data recovery
High Availability
- Use multiple regions for redundancy
- Configure appropriate retry policies
- Implement health checks and monitoring
- Use CDN for media distribution
Cost Optimization
- Choose appropriate storage classes
- Implement lifecycle policies
- Monitor and optimize data transfer
- Use compression where applicable
Roadmap
Completed (Week 6)
- ✅ S3 backend with multipart uploads
- ✅ Azure backend with chunked uploads
- ✅ GCS backend with resumable uploads
- ✅ MinIO self-hosted support
- ✅ B2/Spaces unified backend
- ✅ Docker Compose emulator setup
- ✅ Comprehensive integration tests
Planned (Week 7+)
- 🔲 Migration tool for backend switching
- 🔲 Garbage collection for orphaned objects
- 🔲 Client-side encryption layer
- 🔲 Compression middleware
- 🔲 CDN integration
- 🔲 Metrics and monitoring
- 🔲 Admin CLI tools
Contributing
Running Tests
# Unit tests
# Integration tests (requires Docker)
Code Quality
# Format code
# Run linter
# Check compilation
License
Part of the MediaGit project. See LICENSE in repository root.
Support
- Documentation: TESTING.md
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Version: 0.1.0 Last Updated: 2025-11-14 Status: Week 6 Milestone Complete (Integration Testing)