Managing files
Learn how to manage files in TurboStarter.
Before you start managing files, make sure you have configured storage.
Permissions
Most S3-compatible storage providers allow you to configure bucket permissions and access policies. It's crucial to properly set these up to secure your files and control who can access them.
Here are some key security recommendations:
- Keep your bucket private by default
- Use IAM roles and policies to manage access
- Enable server-side encryption for sensitive data
- Configure CORS settings appropriately for client-side uploads
- Regularly audit bucket permissions and access logs
Making your bucket public is strongly discouraged as it can expose sensitive data and lead to unauthorized access and unexpected costs from bandwidth usage.
For detailed guidance on configuring bucket policies and permissions, refer to your storage provider's documentation:
- AWS S3 Security Documentation
- DigitalOcean Spaces Security
- Cloudflare R2 Security
- Supabase Storage Security
Uploading files
As explained in the overview, TurboStarter uses presigned URLs to upload files to your storage provider.
We prepared a special endpoint to generate presigned URLs for your uploads to use in your client-side code.
Expiration time
The signed URL is only valid for a limited time and will work for anyone who has access to it during that period. Make sure to handle the URL securely and avoid exposing it to unauthorized users.
Then, you can use it to upload files to the generated presigned URL from your frontend code:
The code above demonstrates how to implement file uploads in your application:
-
First, we have a server-side endpoint (
storageRouter
) that generates presigned URLs for uploads. This endpoint:- Requires authentication via
enforceAuth
- Validates the request parameters using
zValidator
- Returns a presigned URL for uploading
- Requires authentication via
-
Then, in the frontend code (
upload.tsx
), we use React Query'suseMutation
hook to handle the upload process:- Requests a presigned URL from the server
- Uploads the file directly to the storage provider using the presigned URL
- Handles success and error cases with toast notifications
This approach ensures secure file uploads while avoiding server bandwidth costs and function timeout issues.
Public uploads
Although it's not recommended to use public uploads in production, you can use the same endpoint to generate presigned URLs for public uploads:
Just remove the enforceAuth
middleware from the endpoint and keep rest of the logic the same.
Displaying files
We provide dedicated endpoints for retrieving signed URLs specifically for displaying files. These URLs are time-limited to maintain security, so they cannot be used for permanent storage or long-term access:
This endpoint is perfect for displaying files that should only be accessible to authorized users for a limited time.
Public files
For displaying files publicly (without authorization and time limitations), you can use the /public
endpoint:
This endpoint generates a public URL for the file that you can use to display in your application. Please ensure that your bucket policy allows public access to the files and verify that you're not exposing any sensitive information.
Deleting files
Deleting files works almost the same way as uploading files. You just need to generate a presigned URL for deletion and then use it to remove the file:
Then, in the frontend code, we use React Query's useMutation
hook to handle the deletion process:
Now that you understand how to manage files in TurboStarter, it's time to build something awesome! Try creating a file upload component, building a photo gallery, or implementing a document management system.
Last updated on