Storage
ShipSaaS uses Cloudflare R2 for file storage, which is accessed directly via Wrangler binding to R2 buckets.
Configure Cloudflare API Token
Section titled “Configure Cloudflare API Token”First, complete the Cloudflare configuration. Ensure that your Token contains Account > Workers R2 Storage > Edit permissions.
Create R2 Bucket
Section titled “Create R2 Bucket”You can create an R2 bucket via either the Cloudflare Dashboard or the Wrangler CLI:
Wrangler CLI
Section titled “Wrangler CLI”- Create a new R2 bucket:
bunx wrangler r2 bucket create your-bucket-nameCloudflare Dashboard
Section titled “Cloudflare 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
Section titled “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
Section titled “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
Section titled “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
Basic File Operations
Section titled “Basic File Operations”ShipSaaS provides a simple API for common file operations:
import { uploadFile, deleteFile, downloadFile, getFileInfo } from '@/storage';
// Upload a fileconst { url, key } = await uploadFile( fileBuffer, 'original-filename.jpg', 'image/jpeg', { folder: 'uploads/images' });
// Delete a fileawait deleteFile(key);
// Download a fileconst stream = await downloadFile(key);
// Get file detailsconst info = await getFileInfo(key);User File Management
Section titled “User File Management”import { listUserFiles, uploadFile } from '@/storage';
// Upload a user fileconst { url, key, metadata } = await uploadFile( file, filename, contentType, { folder: 'userfiles', userId: 'user_123' });
// List all files belonging to a userconst { objects, hasMore, nextCursor } = await listUserFiles('user_123');Best Practices
Section titled “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
Section titled “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