Skip to main content
Sonamu supports multiple storage drivers based on Flydrive. You can use the local file system (fs) and AWS S3 with the same interface.

Supported Drivers

fs (File System)

Local file system

s3 (AWS S3)

AWS S3 compatible storage

fs Driver

Stores files in the local file system.

Basic Configuration

Configuration Options

File storage location
Recommended: Relative path from project root

Practical Examples

Development Environment

Usage:

Static File Serving

You need to serve static files with Fastify:
Result:
When uploads are served from the app origin, a script inside an uploaded HTML or SVG file can call your API with another user’s session. createUploadStaticHeaders({ uploadDir }) adds X-Content-Type-Options: nosniff and Content-Security-Policy: sandbox to files inside uploadDir, and Content-Disposition: attachment to anything that is not an image, audio, or video. Files whose extension maps to a type outside the allow-list, such as .html or .svg, are served as application/octet-stream. Pass an existing setHeaders through its setHeaders option. Serve uploads from a separate domain when you can.When the client-sent MIME type is outside the allow-list (HTML, SVG, XML, and so on), file.extname and the default stream key generator use the bin extension, and saveToDisk and automatic stream uploads store the Content-Type as application/octet-stream. When you call disk.put directly, resolveUploadContentType(mimetype) from sonamu/storage returns the same Content-Type. A custom key generator that uses the original filename still keeps extensions such as .html, so the headers above are required.

s3 Driver

Stores files in AWS S3 or compatible storage.

Basic Configuration

Required Environment Variables

Configuration Options

AWS credentials
How to obtain:
  1. AWS Console → IAM
  2. Create user
  3. Grant S3 permissions (AmazonS3FullAccess)
  4. Create Access Key

Practical Examples

Basic S3 Configuration

Usage:

CloudFront CDN Integration

Benefits:
  • Fast transfer speed (edge locations)
  • Reduced bandwidth costs
  • Caching

Public Bucket Configuration

To make an S3 bucket public:
Caution: Use private buckets for sensitive data

S3 Compatible Storage

Cloudflare R2

Features:
  • Free egress (no transfer costs)
  • S3 API compatible
  • Cloudflare network

MinIO (Self-hosted)

Driver Comparison

Practical Strategies

Development/Production Separation

Multi-Bucket Strategy

Usage:

Cautions

Cautions when configuring drivers:
  1. Environment variable management: Never hardcode
  2. fs urlBuilder required: fs driver requires urlBuilder
  3. S3 permissions check: Grant S3 permissions to IAM user
  4. Bucket name uniqueness: S3 bucket names must be globally unique
  5. Region matching: bucket and region must match

Next Steps

Storage Manager

Managing multiple disks

File Operations

File upload and download

URL Builder

Generating file URLs