Skip to main content
Skip to main content

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.

💾storage

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-service

Environment Variables

STORAGE_TYPERequired

Storage provider to use

Example: local
STORAGE_BASE_URLRequired

Base URL for accessing stored files

Example: http://localhost:3000/uploads
Complete .env example:
STORAGE_TYPE=local
STORAGE_BASE_URL=http://localhost:3000/uploads

Local File Storage

💾storage

Local File Storage

Store files on the local file system with automatic directory management and URL generation

Installation

npm run adapter install storage-local

Environment Variables

STORAGE_TYPERequired

Set to 'local' to use local file storage

Example: local
LOCAL_UPLOAD_DIRRequired

Directory path for storing uploaded files

Example: ./uploads
LOCAL_BASE_URLRequired

Base URL for accessing uploaded files

Example: http://localhost:3000/uploads
LOCAL_MAX_FILE_SIZE

Maximum file size in bytes

Example: 10485760(Default: 5242880)
Complete .env example:
STORAGE_TYPE=local
LOCAL_UPLOAD_DIR=./uploads
LOCAL_BASE_URL=http://localhost:3000/uploads
LOCAL_MAX_FILE_SIZE=10485760

AWS S3 Storage

💾storage

AWS S3 Storage

Scalable cloud storage with AWS S3 including CDN integration and advanced features

Installation

npm run adapter install aws-s3

Environment Variables

STORAGE_TYPERequired

Set to 's3' to use AWS S3 storage

Example: s3
AWS_S3_BUCKETRequired

S3 bucket name for file storage

Example: my-app-uploads
AWS_REGIONRequired

AWS region for the S3 bucket

Example: us-east-1
AWS_ACCESS_KEY_IDRequired

AWS access key ID with S3 permissions

Example: AKIAIOSFODNN7EXAMPLE
AWS_SECRET_ACCESS_KEYRequired

AWS secret access key

Example: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
S3_CDN_URL

CloudFront CDN URL for faster file delivery

Example: https://d1234567890.cloudfront.net
Complete .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.net

Cloudinary Integration

💾storage

Cloudinary Media Management

Advanced media management with automatic optimization, transformations, and CDN delivery

Installation

npm run adapter install cloudinary

Environment Variables

CLOUDINARY_CLOUD_NAMERequired

Your Cloudinary cloud name

Example: my-cloud-name
CLOUDINARY_API_KEYRequired

Cloudinary API key from your dashboard

Example: 123456789012345
CLOUDINARY_API_SECRETRequired

Cloudinary API secret from your dashboard

Example: abcdefghijklmnopqrstuvwxyz123456
CLOUDINARY_FOLDER

Default folder for organizing uploads

Example: my-app
Complete .env example:
CLOUDINARY_CLOUD_NAME=my-cloud-name
CLOUDINARY_API_KEY=123456789012345
CLOUDINARY_API_SECRET=abcdefghijklmnopqrstuvwxyz123456
CLOUDINARY_FOLDER=my-app

File 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:

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