Storage & Cloud Services
Mifty provides flexible storage adapters that support local file storage, cloud storage services, and media management platforms. You can easily switch between different storage providers without changing your application code.
Universal Storage Service Adapter
The universal storage service adapter allows you to switch between local and cloud storage providers through environment configuration, making it perfect for development and production environments.
Universal Storage Service
Universal storage adapter supporting local file system, AWS S3, and other cloud providers with seamless switching
Installation
npm run adapter install storage-serviceEnvironment Variables
STORAGE_TYPERequiredStorage provider to use
localSTORAGE_BASE_URLRequiredBase URL for accessing stored files
http://localhost:3000/uploadsComplete .env example:
STORAGE_TYPE=local
STORAGE_BASE_URL=http://localhost:3000/uploadsLocal File Storage
Local File Storage
Store files on the local file system with automatic directory management and URL generation
Installation
npm run adapter install storage-localEnvironment Variables
STORAGE_TYPERequiredSet to 'local' to use local file storage
localLOCAL_UPLOAD_DIRRequiredDirectory path for storing uploaded files
./uploadsLOCAL_BASE_URLRequiredBase URL for accessing uploaded files
http://localhost:3000/uploadsLOCAL_MAX_FILE_SIZEMaximum file size in bytes
10485760(Default: 5242880)Complete .env example:
STORAGE_TYPE=local
LOCAL_UPLOAD_DIR=./uploads
LOCAL_BASE_URL=http://localhost:3000/uploads
LOCAL_MAX_FILE_SIZE=10485760AWS S3 Storage
AWS S3 Storage
Scalable cloud storage with AWS S3 including CDN integration and advanced features
Installation
npm run adapter install aws-s3Environment Variables
STORAGE_TYPERequiredSet to 's3' to use AWS S3 storage
s3AWS_S3_BUCKETRequiredS3 bucket name for file storage
my-app-uploadsAWS_REGIONRequiredAWS region for the S3 bucket
us-east-1AWS_ACCESS_KEY_IDRequiredAWS access key ID with S3 permissions
AKIAIOSFODNN7EXAMPLEAWS_SECRET_ACCESS_KEYRequiredAWS secret access key
wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEYS3_CDN_URLCloudFront CDN URL for faster file delivery
https://d1234567890.cloudfront.netComplete .env example:
STORAGE_TYPE=s3
AWS_S3_BUCKET=my-app-uploads
AWS_REGION=us-east-1
AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
S3_CDN_URL=https://d1234567890.cloudfront.netCloudinary Integration
Cloudinary Media Management
Advanced media management with automatic optimization, transformations, and CDN delivery
Installation
npm run adapter install cloudinaryEnvironment Variables
CLOUDINARY_CLOUD_NAMERequiredYour Cloudinary cloud name
my-cloud-nameCLOUDINARY_API_KEYRequiredCloudinary API key from your dashboard
123456789012345CLOUDINARY_API_SECRETRequiredCloudinary API secret from your dashboard
abcdefghijklmnopqrstuvwxyz123456CLOUDINARY_FOLDERDefault folder for organizing uploads
my-appComplete .env example:
CLOUDINARY_CLOUD_NAME=my-cloud-name
CLOUDINARY_API_KEY=123456789012345
CLOUDINARY_API_SECRET=abcdefghijklmnopqrstuvwxyz123456
CLOUDINARY_FOLDER=my-appFile Security and Best Practices
Secure File Uploads
// Implement comprehensive file validation
import { FileValidator } from '@mifty/storage-service';
const fileValidator = new FileValidator({
allowedMimeTypes: [
'image/jpeg',
'image/png',
'image/gif',
'application/pdf'
],
maxFileSize: 5 * 1024 * 1024, // 5MB
scanForMalware: true, // Enable virus scanning
validateImageDimensions: {
minWidth: 100,
maxWidth: 4000,
minHeight: 100,
maxHeight: 4000
}
});
// Validate before upload
const validation = await fileValidator.validate(req.file);
if (!validation.isValid) {
return res.status(400).json({
error: validation.errors
});
}
Performance Optimization
// Implement image optimization pipeline
const optimizeImage = async (file) => {
const sharp = require('sharp');
// Auto-optimize based on file type and size
let optimized = sharp(file.buffer);
if (file.size > 1024 * 1024) { // Files > 1MB
optimized = optimized
.resize(1920, 1080, {
fit: 'inside',
withoutEnlargement: true
})
.jpeg({ quality: 80 });
}
return optimized.toBuffer();
};
Backup and Disaster Recovery
// Implement cross-provider backup
const backupService = new BackupService({
primary: 's3',
backup: 'cloudinary',
syncInterval: '24h'
});
// Automatic backup of critical files
await backupService.backup('user-documents/*');
Monitoring and Analytics
Storage Usage Tracking
// Track storage usage and costs
const storageAnalytics = new StorageAnalytics({
providers: ['s3', 'cloudinary'],
trackMetrics: ['usage', 'costs', 'performance']
});
// Generate monthly reports
const report = await storageAnalytics.generateReport('monthly');
console.log('Storage costs:', report.totalCost);
console.log('Most accessed files:', report.topFiles);
Next Steps
After setting up storage solutions, you might want to:
- Configure authentication adapters for user file access control
- Set up email services for file sharing notifications
- Implement payment processing for premium storage features
- Add AI services for automatic file tagging and processing
Migration Between Providers
When you need to migrate from one storage provider to another:
# Use the built-in migration tool
npm run storage:migrate --from=local --to=s3
# Migrate specific folders
npm run storage:migrate --from=local --to=s3 --folder=user-uploads
# Dry run to see what would be migrated
npm run storage:migrate --from=local --to=s3 --dry-run