Skip to main content
Storage drivers can control how URLs are generated for stored files. You can use URL builders to implement CDN, custom domains, Signed URLs, and more.

What is a URL Builder?

A URL builder is a function that converts a file key to a URL.
Example:

fs Driver URL Builder

The fs driver requires urlBuilder configuration.

Basic Configuration

Result:

Custom Domain

Result:
Use case: Separate file server or CDN

Static File Serving

When using the fs driver, the Fastify static plugin is required.
Effect:

S3 Driver URL Builder

The S3 driver generates S3 URLs by default, but can be customized with urlBuilder.

Default Behavior (without urlBuilder)

Result:

CloudFront CDN

To serve files through CDN:
Benefits:
  • Fast transfer speed (edge locations)
  • Reduced bandwidth costs
  • Caching

Custom Domain

Configuration steps:
  1. Create CloudFront distribution
  2. Origin: S3 bucket
  3. CNAME: cdn.example.com
  4. DNS: cdn.example.com β†’ CloudFront

Dynamic URL Generation

You can use environment variables or logic in the URL builder.

Environment-based URL

Result:

Path Transformation

Version Management

Result:

Signed URL (S3)

S3 can generate temporary access URLs.

getSignedUrl()

Result:
Use cases:
  • Temporary sharing of private files
  • Secure downloads
  • Time-limited access

UploadedFile’s signedUrl

Practical Examples

1. CDN + Version Management

Result:

2. Multiple CDNs

Usage:

3. Hash-based URL

Result:

4. Conditional URL

5. Resizing Service

URL Types

Sonamu Storage provides two types of URLs.

Unsigned URL

Regular public URL
Characteristics:
  • Permanent
  • No expiration
  • For public files

Signed URL (S3)

Signed temporary URL
Characteristics:
  • Temporary (with expiration time)
  • Protected by signature
  • For private files
Expiration time options:

Cautions

Cautions when using URL builders:
  1. fs requires urlBuilder: fs driver requires urlBuilder configuration
  2. Static file serving: static plugin required when using fs
  3. Use HTTPS: Use HTTPS URLs in production
  4. Path encoding: Handle special characters
  5. Cache invalidation: Consider CDN cache when changing URLs

CloudFront Configuration

How to use CloudFront with URL builders.

1. Create CloudFront Distribution

  1. AWS Console β†’ CloudFront
  2. Click β€œCreate Distribution”
  3. Origin:
    • Origin Domain: my-bucket.s3.ap-northeast-2.amazonaws.com
    • Origin Path: (leave empty)
  4. Default Cache Behavior:
    • Viewer Protocol Policy: Redirect HTTP to HTTPS
    • Allowed HTTP Methods: GET, HEAD, OPTIONS
  5. Create Distribution

2. Check Domain

After creating the distribution, check the Domain Name:

3. Configure URL Builder

4. Custom Domain (Optional)

  1. Create CNAME record in Route 53:
  2. CloudFront distribution settings:
    • Alternate Domain Names (CNAMEs): cdn.example.com
    • SSL Certificate: Add certificate
  3. Update URL builder:

Next Steps

Storage Manager

Managing multiple disks

Storage Drivers

fs, S3 driver configuration

File Operations

File upload and download