Storage
Learn how to set up and use cloud storage for file uploads and media handling
MkSaaS uses Amazon S3 and compatible services like Cloudflare R2 for file storage and media handling, providing a reliable and scalable solution for storing images and other media files.
Setup
To set up storage in MkSaaS, follow these steps to configure the necessary environment variables:
Cloudflare R2
- Create a Cloudflare account at cloudflare.com
- Create a new R2 bucket:
- Pick a globally unique bucket name (e.g.,
your-project-name
) - Select a region close to your target audience
- Set other options according to your needs
- Pick a globally unique bucket name (e.g.,
- Allow public access to the bucket:
- Settings/Public Access/R2.dev subdomain, click
Allow access
- (Optional) Set custom domains for public access to the bucket
- Save the public access URL as
STORAGE_PUBLIC_URL
- Settings/Public Access/R2.dev subdomain, click
- Create a new API Token:
- R2/API/Manage API Tokens, click
Create User API Token
- Set permissions to
Object Read & Write
to the bucket - Create the API Token, get the
Access Key ID
andSecret Access Key
- R2/API/Manage API Tokens, click
- Set the following environment variables:
- Update the website config to use R2 as the storage provider:
Set storage provider to s3
because Cloudflare R2 is S3-compatible.
Amazon S3
-
Create an AWS account at aws.amazon.com
-
Create a new S3 bucket:
- Pick a globally unique bucket name (e.g.,
your-project-name
) - Select a region close to your target audience
- Disable "Block all public access" if you want files to be publicly accessible
- Set other options according to your needs
- Pick a globally unique bucket name (e.g.,
-
Add a bucket policy to allow public access. Go to the Permissions tab and add:
-
Create an IAM user with S3 access:
- Navigate to the IAM service
- Create a new policy with S3 permissions:
GetObject
,PutObject
,DeleteObject
,ListBucket
- Create a new IAM user with "Programmatic access"
- Attach the policy you created
- Save the Access Key ID and Secret Access Key
-
Add the following environment variables:
- Update the website config to use S3 as the storage provider:
S3-Compatible Alternatives
MkSaaS is compatible with any S3-compatible storage service, including:
- DigitalOcean Spaces - Simple pricing and integrated with DO infrastructure
- Backblaze B2 - Very cost-effective option
- Google Cloud Storage - High performance with Google infrastructure
When using these alternatives, make sure to set the correct endpoint in your environment variables.
If you are setting up the environment, now you can go back to the Environment Setup guide and continue. The rest of this guide can be read later.
Environment Setup
Set up environment variables
Storage System Structure
The storage system in MkSaaS is designed with the following components:
This modular structure makes it easy to extend the storage system with new providers and functionality.
Core Features
- Direct file upload and management for server-side operations
- Browser-side file uploading with size-based optimization
- Pre-signed URLs for secure, direct-to-storage uploads
- Support for Amazon S3 and compatible storage services
- Folder organization for better file management
- Automatic path generation and file naming
- Configurable storage regions and bucket settings
- Support for public and private file storage
Usage
Basic File Operations
MkSaaS provides simple utilities for common file operations:
Browser-Side Uploads
For uploading files directly from the browser, use the uploadFileFromBrowser
function:
This function automatically chooses the best upload method:
- Small files (<10MB): Direct upload through your API endpoint
- Large files (>=10MB): Pre-signed URL for direct upload to S3
Using with Form Components
Here's an example of using the storage system with a form component:
Customization
Creating a Custom Provider
MkSaaS makes it easy to extend the storage system with new providers:
-
Create a new file in the
src/storage/provider
directory -
Implement the
StorageProvider
interface
- Update the provider selection in
index.ts
:
Best Practices
- File Organization: Use folders to organize files by type or purpose (e.g.,
uploads/images
,documents/contracts
) - File Size Limits: Set reasonable file size limits to prevent abuse
- File Type Validation: Validate file types on both client and server sides for security
- Signed URLs: Use pre-signed URLs for private files that require authentication
- CDN Integration: Consider using a CDN in front of your storage for faster delivery
- Backup Strategy: Implement a backup strategy for important files
- Access Control: Use restrictive IAM policies for your storage user
- Error Handling: Properly handle and log storage errors
Next Steps
Now that you understand how to work with file storage in MkSaaS, explore these related topics: