A high-performance image handling service that provides efficient storage, retrieval, and conversion of images. The service automatically converts all images to WebP format for optimal performance and storage efficiency. This service is designed for publicly serving files from securely accessed services like S3. Regardless of the original image format stored in secure S3, it always delivers a compressed WebP version, with memory first efficiency as the top priority.
-
Multi-Storage Support
- Primary storage: Local filesystem
- Secondary storage: S3/MinIO (optional)
- Automatic fallback to secondary storage when files are not found locally
-
Smart Caching
- In-memory cache for the last default 1000 (configured) accessed images
- FIFO (First-In-First-Out) cache eviction policy
- Automatic cache population from storage
-
Efficient File Organization
- Files stored in a hierarchical directory structure based on GUID
- Example: Image ID "123456" is stored as "12/34/56.webp"
- All files stored in WebP format for optimal performance
-
Security Features
- API key authentication
- Rate limiting
- CORS protection
- Secure server configuration
GET /health- Health check endpointGET /images/:id- Get an image by ID
POST /images- Upload a new imageDELETE /images/:id- Delete an imageGET /images- List all images
Create a .env file with the following settings:
# Server Configuration
BIND_ADDRESS=:8080 # Server bind address (e.g., :8080, 0.0.0.0:8080, localhost:8080)
# Storage Configuration
STORAGE_TYPE=local
STORAGE_PATH=./data # Directory where files will be stored
# S3/MinIO Configuration (Optional)
AWS_ACCESS_KEY_ID=minioadmin
AWS_SECRET_ACCESS_KEY=minioadmin
AWS_REGION=us-east-1
AWS_BUCKET_NAME=images
MINIO_ENDPOINT=http://localhost:9000
MINIO_USE_SSL=false
# Security
API_KEY=your_api_key_here
RATE_LIMIT=100
RATE_LIMIT_WINDOW=60
ALLOWED_ORIGINS=http://localhost:3000,https://yourdomain.comThe service uses a hierarchical directory structure for efficient file organization:
data/
├── 12/
│ └── 34/
│ └── 56.webp
├── ab/
│ └── cd/
│ └── ef.webp
└── ...
- Each image is stored with its GUID as the filename
- The GUID is split into parts to create the directory structure
- All files are stored in WebP format
- When retrieving from S3/MinIO, images are automatically converted to WebP
- All images are automatically converted to WebP format
- When requesting an image (e.g.,
xmas.jpg), the service will:- Check cache for any version of the file
- If found in WebP format, return it directly
- If found in another format, convert to WebP and return
- If not in cache, check storage and convert to WebP if needed
- Clone the repository
- Create a
.envfile with your configuration - Build and run the service:
go build -o imageprovider cmd/api/main.go
./imageprovidercurl -X POST http://localhost:8080/images \
-H "X-API-Key: your_api_key" \
-F "file=@/path/to/image.jpg"curl -O http://localhost:8080/images/123456curl -X DELETE http://localhost:8080/images/123456 \
-H "X-API-Key: your_api_key"curl http://localhost:8080/images \
-H "X-API-Key: your_api_key"The service provides clear error messages for common scenarios:
- 401: Invalid or missing API key
- 404: Image not found
- 429: Rate limit exceeded
- 500: Internal server error
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.