Storage
Learn how to set up and use Cloudflare R2 for file uploading and media handling
ShipSaaS uses Cloudflare R2 for file storage, which is accessed directly via Wrangler binding to R2 buckets.
Setup
Configure Cloudflare API Token
First, complete the Cloudflare configuration. Ensure that your Token contains Account > Workers R2 Storage > Edit permissions.
Create R2 Bucket
You can create an R2 bucket via either the Cloudflare Dashboard or the Wrangler CLI:
Wrangler CLI
- Create a new R2 bucket:
bunx wrangler r2 bucket create your-bucket-nameCloudflare Dashboard
- Log in to the Cloudflare Dashboard
- Navigate to Storage & Databases > R2 Object Storage
- Click Create Bucket
- Enter a globally unique bucket name
- Select a region close to your target audience
- Click Create Bucket
Configure Wrangler Binding
Configure the R2 bucket binding in your wrangler.jsonc file:
wrangler.jsonc
{
"r2_buckets": [
{
"binding": "BUCKET",
"bucket_name": "your-bucket-name" // Make sure to modify this to your bucket name
}
]
}Update Website Configuration
In general, you can keep the default configurations. If you need to make changes, you can modify the storage config in src/config/website.ts:
src/config/website.ts
export const websiteConfig = {
// ...other configurations
storage: {
enable: true,
provider: 'r2',
maxFileSize: 10 * 1024 * 1024,
allowedTypes: [],
userFilesFolder: 'userfiles',
},
// ...other configurations
}If you are setting up your environment, you can now return to the Environment Configuration document and continue. The remainder of this document can be read later.
Environment configuration set environment variables
Core Features
- File uploading and downloading
- Using folders to organize files
- Automated file path generation and naming
- File information query and user file list
- Configurable file size limits and type restrictions
Usage
Basic File Operations
ShipSaaS provides a simple API for common file operations:
import { uploadFile, deleteFile, downloadFile, getFileInfo } from '@/storage';
// Upload a file
const { url, key } = await uploadFile(
fileBuffer,
'original-filename.jpg',
'image/jpeg',
{ folder: 'uploads/images' }
);
// Delete a file
await deleteFile(key);
// Download a file
const stream = await downloadFile(key);
// Get file details
const info = await getFileInfo(key);User File Management
import { listUserFiles, uploadFile } from '@/storage';
// Upload a user file
const { url, key, metadata } = await uploadFile(
file,
filename,
contentType,
{ folder: 'userfiles', userId: 'user_123' }
);
// List all files belonging to a user
const { objects, hasMore, nextCursor } = await listUserFiles('user_123');Best Practices
- File Organization: Use folders to organize files by type or purpose.
- File Size Limits: Set reasonable file size limits to prevent abuse.
- File Type Validation: Validate file types on both the client and server sides for enhanced security.
Next Steps
Now that you know how to use file storage in ShipSaaS, explore these related topics:
-
Payment - Configure website payment features
-
Authentication - Configure user authentication
-
Database - Configure database
-
Environment Configuration - Configure environment variables