What is a URL Builder?
A URL builder is a function that converts a file key to a URL.fs Driver URL Builder
The fs driver requires urlBuilder configuration.Basic Configuration
Custom Domain
Static File Serving
When using the fs driver, the Fastify static plugin is required.S3 Driver URL Builder
The S3 driver generates S3 URLs by default, but can be customized with urlBuilder.Default Behavior (without urlBuilder)
CloudFront CDN
To serve files through CDN:- Fast transfer speed (edge locations)
- Reduced bandwidth costs
- Caching
Custom Domain
- Create CloudFront distribution
- Origin: S3 bucket
- CNAME: cdn.example.com
- DNS: cdn.example.com → CloudFront
Dynamic URL Generation
You can use environment variables or logic in the URL builder.Environment-based URL
Path Transformation
Version Management
Signed URL (S3)
S3 can generate temporary access URLs.getSignedUrl()
- Temporary sharing of private files
- Secure downloads
- Time-limited access
UploadedFile’s signedUrl
Practical Examples
1. CDN + Version Management
2. Multiple CDNs
3. Hash-based URL
4. Conditional URL
5. Resizing Service
URL Types
Sonamu Storage provides two types of URLs.Unsigned URL
Regular public URL- Permanent
- No expiration
- For public files
Signed URL (S3)
Signed temporary URL- Temporary (with expiration time)
- Protected by signature
- For private files
Cautions
CloudFront Configuration
How to use CloudFront with URL builders.1. Create CloudFront Distribution
- AWS Console → CloudFront
- Click “Create Distribution”
- Origin:
- Origin Domain:
my-bucket.s3.ap-northeast-2.amazonaws.com - Origin Path: (leave empty)
- Origin Domain:
- Default Cache Behavior:
- Viewer Protocol Policy: Redirect HTTP to HTTPS
- Allowed HTTP Methods: GET, HEAD, OPTIONS
- Create Distribution
2. Check Domain
After creating the distribution, check the Domain Name:3. Configure URL Builder
4. Custom Domain (Optional)
-
Create CNAME record in Route 53:
-
CloudFront distribution settings:
- Alternate Domain Names (CNAMEs):
cdn.example.com - SSL Certificate: Add certificate
- Alternate Domain Names (CNAMEs):
-
Update URL builder: