Cloud Storage
Bunship uses AWS S3 compatible file storage systems
Bunship integrates with AWS S3 compatible cloud storage solutions for storing and managing various files such as user-uploaded images, documents, videos, and more. In addition to Amazon S3, Bunship also supports other storage services compatible with the S3 API, including MinIO, DigitalOcean Spaces, Cloudflare R2, and others.
Environment Configuration
To enable cloud storage functionality, configure the following environment variables in your .env file:
Configuration Examples for Different Service Providers
Amazon S3
MinIO
Usage
Bunship provides an encapsulated S3 service class that can generate temporary upload URLs on the server side, allowing the frontend to upload files directly to cloud storage without going through the server.
S3 Server Configuration
1. Using Pre-signed URLs for File Upload
Bunship has implemented an S3 service class in @/lib/s3. Here's an example of server-side operations to generate pre-signed URLs:
This function returns:
putUrl: Temporary signed URL for uploading filesurl: URL for accessing the file after uploadendpoint: S3 endpoint URLcompletedUrl: Complete file access URL
2. Server-side Direct File Upload
If you need to upload files directly from the server:
This function returns:
path: File path in the bucketpathWithFilename: Complete file key namefilename: FilenamecompletedUrl: Complete file access URLbaseUrl: Base storage URLmime: File MIME typebucket: Bucket name
Frontend File Upload Example
Here's how to use the server-side operations to upload files from the frontend:
Advanced Use Cases
Image Upload and Preview
Using avatar upload as an example, here's how to handle image upload and preview:
Security Considerations
The main advantages of using pre-signed URLs are:
- No need to expose sensitive S3 credentials to the client
- Precise control over permissions and expiration for each upload
- Server-side validation of file types, sizes, etc.
In production environments, we recommend:
- Configuring appropriate CORS policies
- Using temporary credentials instead of long-term ones
- Setting reasonable expiration times for signed URLs
- Implementing size limits for uploaded files