CanxJS provides a powerful file storage abstraction called "Flydrive". Easily switch between local disk, S3, or R2 without changing your code.
Storage configuration is located in src/config/app.ts. You can define multiple "disks" using different drivers.
export const config = {
storage: {
default: 'local',
disks: {
local: {
driver: 'local',
root: './storage/app'
},
s3: {
driver: 's3',
key: process.env.AWS_ACCESS_KEY_ID,
secret: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_DEFAULT_REGION,
bucket: process.env.AWS_BUCKET,
}
}
}
}The storage facade allows you to interact with your configured disks.
import { storage } from 'canxjs';
// Write to default disk
await storage.put('avatars/1.jpg', fileContent);
// Write to specific disk
await storage.disk('s3').put('avatars/1.jpg', fileContent);
// Check existence
if (await storage.exists('file.jpg')) {
// ...
}
// Get URL
const url = storage.url('avatars/1.jpg');CanxJS simplifies file uploads with the handleUpload helper, which handles validation, naming, and storage automatically.
import { handleUpload } from 'canxjs';
class UserController extends Controller {
@Post('/avatar')
async uploadAvatar(req: CanxRequest) {
const file = await handleUpload(req, 'avatar', {
directory: 'avatars',
allowedTypes: ['image/jpeg', 'image/png'],
maxSize: 1024 * 1024 * 5 // 5MB
});
if (!file) throw new Error('No file uploaded');
return { url: file.url };
}
}put(path, content)get(path)exists(path)delete(path)copy(from, to)move(from, to)url(path)size(path)mimeType(path)