Raw
1 import S3BucketAcl from "../../../troubleshooting/_sections/s3/aws-bucket-acl.mdx"
2 import CloudflareChecksum from "../../../troubleshooting/_sections/s3/cloudflare-checksum.mdx"
3
4 import {
5 Table,
6 Tabs,
7 TabsList,
8 TabsContent,
9 TabsContentWrapper,
10 TabsTrigger,
11 DetailsList,
12 } from "docs-ui"
13
14 export const metadata = {
15 title: `S3 File Module Provider`,
16 }
17
18 # {metadata.title}
19
20 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.
21
22 <Note title="Looking for managed storage?">
23
24 Cloud offers a managed file storage solution with AWS S3 for your Medusa application. Refer to the [S3](!cloud!/s3) Cloud documentation for more details.
25
26 </Note>
27
28 ## Prerequisites
29
30 <Tabs defaultValue="aws">
31 <TabsList>
32 <TabsTrigger value="aws">AWS S3</TabsTrigger>
33 <TabsTrigger value="minio">MinIO</TabsTrigger>
34 <TabsTrigger value="spaces">DigitalOcean Spaces</TabsTrigger>
35 <TabsTrigger value="supabase">Supabase S3 Storage</TabsTrigger>
36 <TabsTrigger value="cloudflare">Cloudflare R2</TabsTrigger>
37 </TabsList>
38 <TabsContentWrapper>
39 <TabsContent value="aws">
40
41 - [AWS account](https://console.aws.amazon.com/console/home?nc2=h_ct&src=header-signin).
42 - Create [AWS user with AmazonS3FullAccess permissions](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-and-attach-iam-policy.html).
43 - Create [AWS user access key ID and secret access key](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html#Using_CreateAccessKey).
44 - Create [S3 bucket](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html) with the "Public Access setting" enabled:
45 1. On your bucket's dashboard, click on the Permissions tab.
46 2. Click on the Edit button of the Block public access (bucket settings) section.
47 3. In the form that opens, don't toggle any checkboxes and click the "Save changes" button.
48 4. Confirm saving the changes by entering `confirm` in the pop-up that shows.
49 5. Back on the Permissions page, scroll to the Object Ownership section and click the Edit button.
50 6. In the form that opens:
51 - Choose the "ACLs enabled" card.
52 - Click on the "Save changes" button.
53 7. Back on the Permissions page, scroll to the "Access Control List (ACL)" section and click on the Edit button.
54 8. In the form that opens, enable the Read permission for "Everyone (public access)".
55 9. Check the "I understand the effects of these changes on my objects and buckets." checkbox.
56 10. Click on the "Save changes" button.
57
58 </TabsContent>
59 <TabsContent value="minio">
60
61 - [Install MinIO](https://min.io/docs/minio/linux/index.html).
62 - Change port to `9001` using the [console address](https://min.io/docs/minio/linux/reference/minio-server/minio-server.html#minio.server.-console-address) and [address](https://min.io/docs/minio/linux/reference/minio-server/minio-server.html#minio.server.-address) CLI options.
63 - Create MinIO access and secret access key:
64 - Go to User -> Access Keys
65 - Click on the Create Access Keys button.
66 - Click on the Create button.
67 - Copy the keys of the pop-up. Make sure to copy the secret key as it won't be shown again.
68 - Create a [MinIO bucket](https://min.io/docs/minio/linux/administration/console/managing-objects.html#creating-buckets) with public access policy. To add the policy:
69 - Go to the bucket's dashboard from Administrator -> Buckets.
70 - Under the Summary section, click on the pencil icon next to Access Policy.
71 - In the pop-up, choose Custom from the dropdown.
72 - In the editor, enter the following:
73
74 ```json highlights={[["15", "{bucketname}", "Replace with the bucket's name."]]}
75 {
76 "Version": "2012-10-17",
77 "Statement": [
78 {
79 "Action": [
80 "s3:GetObject"
81 ],
82 "Effect": "Allow",
83 "Principal": {
84 "AWS": [
85 "*"
86 ]
87 },
88 "Resource": [
89 "arn:aws:s3:::{bucketname}/*"
90 ]
91 }
92 ]
93 }
94 ```
95
96 Make sure to replace `{bucket_name}` with the name of the bucket you created. You can edit the access policy under the Summary section of your bucket's dashboard.
97
98 </TabsContent>
99 <TabsContent value="spaces">
100
101 - Create [DigitalOcean account](https://cloud.digitalocean.com/registrations/new).
102 - Create [DigitalOcean Spaces bucket](https://docs.digitalocean.com/products/spaces/how-to/create/).
103 - Create [DigitalOcean Spaces access and secret access keys](https://docs.digitalocean.com/products/spaces/how-to/manage-access/#access-keys).
104
105 </TabsContent>
106 <TabsContent value="supabase">
107
108 - [Supabase account](https://supabase.com/dashboard/sign-in) with a project.
109 - Create [New Public S3 bucket](https://supabase.com/docs/guides/storage/buckets/creating-buckets?queryGroups=language&language=dashboard).
110 - Create [New S3 Access Keys](https://supabase.com/docs/guides/storage/s3/authentication?queryGroups=language&language=javascript#s3-access-keys).
111 - Create [Storage Policy](https://supabase.com/docs/guides/storage/security/access-control).
112 1. On your bucket's dashboard, click on Policies under the Configuration sidebar.
113 2. Click on New Policy under "Other policies under storage.objects".
114 3. Click get started quickly, choose "insert access for authenticated users only", and click the "Use this template" button.
115 4. Click ALL for "Allowed Operations", click the "Review" button, then the "Save policy" button.
116
117 </TabsContent>
118 <TabsContent value="cloudflare">
119 1. Create a [Cloudflare account](https://dash.cloudflare.com/sign-up).
120 2. Set up your R2 bucket:
121 - Navigate to R2 Object Storage in your dashboard. You may need to provide your credit-card information.
122 - Click "Create bucket"
123 - Enter a unique bucket name
124 - Select "Automatic" for location
125 - Choose "Standard" for storage class
126 - Confirm by clicking "Create bucket"
127 3. Configure public access:
128 - Make sure you have a [domain configured in your Cloudflare account](https://developers.cloudflare.com/dns/manage-dns-records/how-to/create-dns-records/).
129 - On your bucket's dashboard, click on the Settings tab.
130 - In the General Section look for Custom Domains (recommended for production use)
131 - Click on the Add button to add your domain name.
132 - Enter the domain name you want to connect to and select Continue.
133 - Review the new record that will be added to the DNS table and select Connect Domain.
134 4. Retrieve credentials:
135 - [Go to API tokens page](https://dash.cloudflare.com/?to=/:account/r2/api-tokens):
136 - Click "Create User API token"
137 - Edit the "R2 Token" name
138 - Under Permissions, select Object Read & Write permission types
139 - You can optionally specify the buckets that this API token has access to under the "Specify bucket(s)" section.
140 - Once done, click the "Create User API Token" button.
141 - Copy the jurisdiction-specific endpoint for S3 clients to S3_ENDPOINT into your environment variables.
142 - Copy the Access Key ID and Secret Access Key to the corresponding fields into your environment variables.
143 - Copy your custom domain to `S3_FILE_URL` with leading https:// into your environment variables.
144
145 </TabsContent>
146
147 </TabsContentWrapper>
148 </Tabs>
149
150 ---
151
152 ## Register the S3 File Module
153
154 Add the module into the `providers` array of the File Module:
155
156 <Note>
157
158 The File Module accepts one provider only.
159
160 </Note>
161
162 ```ts title="medusa-config.ts"
163 import { Modules } from "@medusajs/framework/utils"
164
165 // ...
166
167 module.exports = {
168 // ...
169 modules: [
170 // ...
171 {
172 resolve: "@medusajs/medusa/file",
173 options: {
174 providers: [
175 {
176 resolve: "@medusajs/medusa/file-s3",
177 id: "s3",
178 options: {
179 file_url: process.env.S3_FILE_URL,
180 access_key_id: process.env.S3_ACCESS_KEY_ID,
181 secret_access_key: process.env.S3_SECRET_ACCESS_KEY,
182 region: process.env.S3_REGION,
183 bucket: process.env.S3_BUCKET,
184 endpoint: process.env.S3_ENDPOINT,
185 // other options...
186 },
187 },
188 ],
189 },
190 },
191 ],
192 }
193 ```
194
195 ### Additional Configuration for MinIO and Supabase
196
197 If you're using MinIO or Supabase, set `forcePathStyle` to `true` in the `additional_client_config` object.
198
199 For example:
200
201 ```ts title="medusa-config.ts"
202 module.exports = defineConfig({
203 // ...
204 modules: [
205 {
206 resolve: "@medusajs/medusa/file",
207 options: {
208 providers: [
209 {
210 resolve: "@medusajs/medusa/file-s3",
211 id: "s3",
212 options: {
213 // ...
214 additional_client_config: {
215 forcePathStyle: true,
216 },
217 },
218 },
219 ],
220 },
221 },
222 ],
223 })
224 ```
225
226 ### S3 File Module Options
227
228 <Table>
229 <Table.Header>
230 <Table.Row>
231 <Table.HeaderCell>
232 Option
233 </Table.HeaderCell>
234 <Table.HeaderCell>
235 Description
236 </Table.HeaderCell>
237 <Table.HeaderCell>
238 Default
239 </Table.HeaderCell>
240 </Table.Row>
241 </Table.Header>
242 <Table.Body>
243 <Table.Row>
244 <Table.Cell>
245
246 `file_url`
247
248 </Table.Cell>
249 <Table.Cell>
250
251 The base URL to upload files to.
252
253 - For AWS S3, the endpoint is of the format `https://{bucket}.s3.{region}.amazonaws.com`
254 - For MinIO, it's the URL to the MinIO server with the bucket's name. For example, `https://{server-domain}/{bucket}`. Locally, it may be something like `http://192.168.0.123:9001/{bucket}`.
255 - For DigitalOcean Spaces, it's either the Origin Endpoint or the CDN endpoint of your Spaces Object Storage bucket.
256 - for Supabase, it's `https://{uniqueID}.supabase.co/storage/v1/object/public/{bucket}`. You can retrieve the `uniqueID` from [Storage Settings](https://supabase.com/docs/guides/storage/s3/authentication?queryGroups=language&language=javascript#s3-access-keys) page in the Endpoint URL.
257 - For Cloudflare R2, it's your `Custom Domain` (recommended for production use) or `Public R2.dev Bucket URL` (for development).
258
259 </Table.Cell>
260 <Table.Cell>
261
262 \-
263
264 </Table.Cell>
265 </Table.Row>
266 <Table.Row>
267 <Table.Cell>
268
269 `access_key_id`
270
271 </Table.Cell>
272 <Table.Cell>
273
274 The AWS or (S3 compatible) user's access key ID.
275
276 </Table.Cell>
277 <Table.Cell>
278
279 \-
280
281 </Table.Cell>
282 </Table.Row>
283 <Table.Row>
284 <Table.Cell>
285
286 `secret_access_key`
287
288 </Table.Cell>
289 <Table.Cell>
290
291 The AWS or (S3 compatible) user's secret access key.
292
293 </Table.Cell>
294 <Table.Cell>
295
296 \-
297
298 </Table.Cell>
299 </Table.Row>
300 <Table.Row>
301 <Table.Cell>
302
303 `region`
304
305 </Table.Cell>
306 <Table.Cell>
307
308 The bucket's region code.
309
310 For MinIO, use `us-east-1`.
311
312 For Cloudflare, use `auto`.
313
314 </Table.Cell>
315 <Table.Cell>
316
317 \-
318
319 </Table.Cell>
320 </Table.Row>
321 <Table.Row>
322 <Table.Cell>
323
324 `bucket`
325
326 </Table.Cell>
327 <Table.Cell>
328
329 The bucket's name.
330
331 </Table.Cell>
332 <Table.Cell>
333
334 \-
335
336 </Table.Cell>
337 </Table.Row>
338 <Table.Row>
339 <Table.Cell>
340
341 `endpoint`
342
343 </Table.Cell>
344 <Table.Cell>
345
346 The URL to the AWS S3 (or compatible S3 API) server.
347
348 - For AWS S3, the endpoint is of the format `https://s3.{region}.amazonaws.com`
349 - For MinIO, it's the URL to the MinIO server. For example, locally, it may be something like `http://192.168.0.123:9001`.
350 - For DigitalOcean Spaces, it's the Spaces Origin Endpoint of the format `https://{region}.digitaloceanspaces.com`.
351 - For Supabase, it's the Endpoint URL in the [Storage Settings](https://supabase.com/docs/guides/storage/s3/authentication?queryGroups=language&language=javascript#s3-access-keys).
352 - For Cloudflare, it's the endpoint for S3 clients `https://{your-account-id}.r2.cloudflarestorage.com`.
353
354 </Table.Cell>
355 <Table.Cell>
356
357 \-
358
359 </Table.Cell>
360 </Table.Row>
361 <Table.Row>
362 <Table.Cell>
363
364 `prefix`
365
366 </Table.Cell>
367 <Table.Cell>
368
369 A string to prefix each uploaded file's name.
370
371 </Table.Cell>
372 <Table.Cell>
373
374 \-
375
376 </Table.Cell>
377 </Table.Row>
378 <Table.Row>
379 <Table.Cell>
380
381 `cache_control`
382
383 </Table.Cell>
384 <Table.Cell>
385
386 A string indicating how long objects remain in the AWS S3 (or compatible S3 API) cache.
387
388 </Table.Cell>
389 <Table.Cell>
390
391 `public, max-age=31536000`
392
393 </Table.Cell>
394 </Table.Row>
395 <Table.Row>
396 <Table.Cell>
397
398 `download_file_duration`
399
400 </Table.Cell>
401 <Table.Cell>
402
403 A number indicating the expiry time of presigned URLs in seconds.
404
405 </Table.Cell>
406 <Table.Cell>
407
408 `3600` (An hour)
409
410 </Table.Cell>
411 </Table.Row>
412 <Table.Row>
413 <Table.Cell>
414
415 `additional_client_config`
416
417 </Table.Cell>
418 <Table.Cell>
419
420 Any additional configurations to pass to the S3 client.
421
422 Refer to [this AWS API reference](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html) for a full list of accepted configuration.
423
424 </Table.Cell>
425 <Table.Cell>
426
427 \-
428
429 </Table.Cell>
430 </Table.Row>
431
432 </Table.Body>
433 </Table>
434
435 ---
436
437 ## Troubleshooting
438
439 <DetailsList
440 sections={[
441 {
442 title:
443 "AWS: The bucket does not allow ACLs (Enabling public access to bucket)",
444 content: <S3BucketAcl />,
445 },
446 {
447 title: "Cloudflare: Checksum error",
448 content: <CloudflareChecksum />,
449 },
450 ]}
451 />