npx vratix init
✅
Large File Support
✅
Direct-to-storage File Streaming
About
The S3 File Storage API module provides a secure and efficient solution for storing files of any size directly in an AWS S3 bucket. This module requires an existing AWS account and aims to facilitate seamless integration with minimal configuration.
Installation
To add the S3 File Storage Module to your project, run:
.env
Add the following environment variables to your .env
file:
- S3_BUCKET_NAME: The S3 bucket where files will be stored
- Default: None (required)
- Example:
S3_BUCKET_NAME=my-s3-bucket
- S3_BUCKET_REGION: The AWS Region of your S3 bucket
- Default: None (required)
- Example:
S3_BUCKET_REGION=us-east-1
Dependencies
This module will install the auth-basic
API module for user authentication and the protectedRoute
middleware.
Usage
To use this module, import the router from @/routes/storeFileS3.js
into your entry point file (e.g., server.ts
):
Authentication
Note: Refer to the Node.js AWS SDK documentation for authentication best practices. The module automatically retrieves AWS credentials from your environment.
We reccomend you use AWS IAM Identity Center (SSO) for local development. After you complete the AWS SSO configuration you will need to update the SSO profile name in src/routes/storeFileS3.ts
:
File Uploads
The module uses formidable
for parsing and streaming files to S3. The file should be sent with multipart/form-data
content type.
Each file is stored with a unique key in the format
owner:${userId}_name:${fileName}
to associate files with the uploading user. Returned file names are simplified tofileName
only. You can adjust this logic as needed. NOTE: Make sure to include the file extension when specifyingfileName
.
Below is an example React component that allows users to upload files directly from a front-end form:
Endpoints
The S3 File Storage Module exposes the following endpoints:
Method | Endpoint | Description |
---|---|---|
POST | /upload/:fileName | Uploads a file to the designated S3 bucket |
GET | /files | Retrieves a list of uploaded files for the current user |
DELETE | /files | Deletes a specific file associated with the user |
GET | /:fileKey | Downloads a file by name |
DELETE | /:fileKey | Deletes a file by name |
Errors
Below are common errors with solutions for this module:
Error Code | Name | Solution |
---|---|---|
404 | FileNotFound | The requested file does not exist in the S3 bucket. Verify the file's existence and ensure the correct file key is used |
400 | ErrorDownloadingFile | An error occurred while downloading the file from S3. Check AWS credentials and network configuration in your environment |
400 | CantGetFiles | An error occurred while retrieving the file list from S3. Verify AWS credentials and check the bucket permissions |
Examples
To explore sample requests and responses, download our Postman collection: