Crypsol Storage
A production-ready AWS S3 storage library for Rust services with image processing, validation, and thumbnail generation.
Features
- Image Upload with Processing: Automatic resizing and thumbnail generation
- Multiple Format Support: JPEG, PNG, GIF, WebP
- High-Quality Resizing: Uses Lanczos3 filter for optimal quality
- Configurable Dimensions: Customize image and thumbnail sizes
- File Validation: Content-type and file size validation
- Presigned URLs: Generate temporary access URLs for private objects
- Async/Await: Built on Tokio for high-performance async operations
- Serde Support: Optional serialization support for API responses
Installation
Add to your Cargo.toml:
[]
= "0.1"
With Serde support:
[]
= { = "0.1", = ["serde"] }
Environment Variables
| Variable | Required | Default | Description |
|---|---|---|---|
S3_AWS_ACCESS_KEY |
Yes | - | AWS Access Key ID |
S3_AWS_SECRET_KEY |
Yes | - | AWS Secret Access Key |
S3_AWS_REGION |
No | us-east-1 |
AWS Region |
S3_BUCKET_NAME |
No | crypsol-storage |
S3 Bucket Name |
S3_PUBLIC_BASE_URL |
No | Auto-generated | CDN or custom domain URL |
S3_MAX_FILE_SIZE_MB |
No | 5 |
Maximum file size in MB |
Quick Start
Upload Image with Thumbnail
use ;
async
Upload with Custom Dimensions
use ;
let config = ImageUploadConfig ;
let result = upload_image_with_config.await?;
Upload with Custom S3 Key
use upload_image_with_key;
// Full control over the S3 path
let result = upload_image_with_key.await?;
Upload Raw File (No Processing)
use upload_file;
let pdf_data = read?;
let result = upload_file.await?;
println!;
Delete Image with Thumbnail
use delete_image_with_thumbnail;
// Deletes both main image and thumbnail
delete_image_with_thumbnail.await?;
Check if File Exists
use file_exists;
if file_exists.await?
Generate Presigned URL
use generate_presigned_url;
// Generate URL valid for 1 hour (3600 seconds)
let url = generate_presigned_url.await?;
println!;
Extract Key from URL
use extract_key_from_url;
let url = "https://bucket.s3.us-east-1.amazonaws.com/profiles/image.jpg";
if let Some = extract_key_from_url
Validation
Content Type Validation
use ;
// Check if content type is allowed
validate_content_type?; // Ok
validate_content_type?; // Error
// Allowed types: image/jpeg, image/png, image/gif, image/webp
println!;
File Size Validation
use ;
let file_size = 2 * 1024 * 1024; // 2MB
validate_file_size?; // Ok if under limit
println!;
Error Handling
use Error;
match upload_image_with_config.await
S3 Bucket Configuration
Required IAM Permissions
CORS Configuration (for browser uploads)
License
MIT License - see LICENSE for details.
Author
Zuhair Thabit - admin@crypsol.tech