Sonamu provides an integrated storage system for file uploads and storage. It supports local file system (fs) and AWS S3, and you can easily switch between them based on the environment.
When saving files, you explicitly specify which driver to use in the saveToDisk(diskName, key) call. To use different drivers per environment, branch at the code level using environment variables.
fs Driver
Stores files on the local file system. Suitable for development environments.
DriverKey currently supports only "fs" and "s3". Multiple bucket configurations — such as separating public and private files into different buckets — are not supported at this time.
File Upload Usage
After storage configuration, you can upload files using the @upload decorator and BufferedFile.saveToDisk().
import { BaseModelClass, Sonamu, upload } from "sonamu";import { type DriverKey } from "sonamu/storage";export class FileModel extends BaseModelClass { @upload() static async upload() { const ctx = Sonamu.getContext(); const file = ctx.bufferedFiles[0]; if (!file) throw new Error("No uploaded file found."); // Select driver via environment variable const diskName: DriverKey = process.env.DRIVE_DISK === "s3" ? "s3" : "fs"; const url = await file.saveToDisk(diskName, `uploads/${Date.now()}-${file.filename}`); return { url }; }}