S3 File Module Provider
The S3 File Module Provider integrates Amazon S3 and services following a compatible API (such as MinIO or DigitalOcean Spaces) to store files uploaded to your Medusa application.
Set Up on Cloud#
Cloud provisions managed S3 storage for each environment and configures the S3 File Module Provider to use it. So, you don't configure the provider or create a bucket yourself.
To deploy your application with the managed S3 File Module Provider:
- Remove the S3 File Module Provider from
medusa-config.tsif you registered it, to avoid conflicts with the provider that Cloud configures. - Commit your changes and push them to the branch that your Cloud environment tracks. Cloud then creates a deployment that stores uploaded files in the environment's S3 storage.
Prerequisites#
- AWS account.
- Create AWS user with AmazonS3FullAccess permissions.
- Create AWS user access key ID and secret access key.
- Create S3 bucket with the "Public Access setting" enabled:
- On your bucket's dashboard, click on the Permissions tab.
- Click on the Edit button of the Block public access (bucket settings) section.
- In the form that opens, don't toggle any checkboxes and click the "Save changes" button.
- Confirm saving the changes by entering
confirmin the pop-up that shows. - Back on the Permissions page, scroll to the Object Ownership section and click the Edit button.
- In the form that opens:
- Choose the "ACLs enabled" card.
- Click on the "Save changes" button.
- Back on the Permissions page, scroll to the "Access Control List (ACL)" section and click on the Edit button.
- In the form that opens, enable the Read permission for "Everyone (public access)".
- Check the "I understand the effects of these changes on my objects and buckets." checkbox.
- Click on the "Save changes" button.
Register the S3 File Module#
Add the module into the providers array of the File Module:
import { Modules } from "@medusajs/framework/utils"
// ...
module.exports = {
// ...
modules: [
// ...
{
resolve: "@medusajs/medusa/file",
options: {
providers: [
{
resolve: "@medusajs/medusa/file-s3",
id: "s3",
options: {
file_url: process.env.S3_FILE_URL,
access_key_id: process.env.S3_ACCESS_KEY_ID,
secret_access_key: process.env.S3_SECRET_ACCESS_KEY,
region: process.env.S3_REGION,
bucket: process.env.S3_BUCKET,
endpoint: process.env.S3_ENDPOINT,
// other options...
},
},
],
},
},
],
}
Environment Variables#
On Cloud, you don't need to set any environment variables, since Cloud manages the S3 storage for you.
For local development, add the environment variables that the provider's options use to your .env file:
S3_FILE_URL=<YOUR_S3_FILE_URL>
S3_ACCESS_KEY_ID=<YOUR_S3_ACCESS_KEY_ID>
S3_SECRET_ACCESS_KEY=<YOUR_S3_SECRET_ACCESS_KEY>
S3_REGION=<YOUR_S3_REGION>
S3_BUCKET=<YOUR_S3_BUCKET>
S3_ENDPOINT=<YOUR_S3_ENDPOINT>
Additional Configuration for MinIO and Supabase#
If you're using MinIO or Supabase, set forcePathStyle to true in the additional_client_config object.
For example:
module.exports = defineConfig({
// ...
modules: [
{
resolve: "@medusajs/medusa/file",
options: {
providers: [
{
resolve: "@medusajs/medusa/file-s3",
id: "s3",
options: {
// ...
additional_client_config: {
forcePathStyle: true,
},
},
},
],
},
},
],
})
S3 File Module Options#
Option | Description | Default |
|---|---|---|
| The base URL to upload files to.
| - |
| The AWS or (S3 compatible) user's access key ID. | - |
| The AWS or (S3 compatible) user's secret access key. | - |
| The bucket's region code. For MinIO, use For Cloudflare, use | - |
| The bucket's name. | - |
| The URL to the AWS S3 (or compatible S3 API) server.
| - |
| A string to prefix each uploaded file's name. | - |
| A string indicating how long objects remain in the AWS S3 (or compatible S3 API) cache. |
|
| A number indicating the expiry time of presigned URLs in seconds. |
|
| Any additional configurations to pass to the S3 client. Refer to this AWS API reference for a full list of accepted configuration. | - |
Restrict Presigned Upload URLs to a File Size#
The File Module's getUploadFileUrls method returns a presigned URL that a client uses to upload a file directly to S3. When you pass the size property, the S3 File Module Provider signs the file size, in bytes, into the URL. S3 then rejects any upload whose size is different from the signed size.
For example:
import { Modules } from "@medusajs/framework/utils"
// in a step...
const fileModuleService = container.resolve(
Modules.FILE
)
const { url, key } = await fileModuleService.getUploadFileUrls({
filename: "image.png",
mimeType: "image/png",
size: 1024,
})