aws-s3-multipart.mdx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. ---
  2. sidebar_position: 4
  3. slug: /aws-s3
  4. ---
  5. import Tabs from '@theme/Tabs';
  6. import TabItem from '@theme/TabItem';
  7. import UppyCdnExample from '/src/components/UppyCdnExample';
  8. # AWS S3
  9. The `@uppy/aws-s3` plugin can be used to upload files directly to a S3 bucket or
  10. a S3-compatible provider, such as Google Cloud Storage or DigitalOcean Spaces.
  11. Uploads can be signed using either [Companion][companion docs], temporary
  12. credentials, or a custom signing function.
  13. ## When should I use it?
  14. :::tip
  15. Not sure which uploader is best for you? Read
  16. “[Choosing the uploader you need](/docs/guides/choosing-uploader)”.
  17. :::
  18. You can use this plugin when you prefer a _client-to-storage_ over a
  19. _client-to-server-to-storage_ (such as [Transloadit](/docs/transloadit) or
  20. [Tus](/docs/tus)) setup. This may in some cases be preferable, for instance, to
  21. reduce costs or the complexity of running a server and load balancer with
  22. [Tus](/docs/tus).
  23. Multipart uploads start to become valuable for larger files (100 MiB+) as
  24. it uploads a single object as a set of parts. This has certain benefits, such as
  25. improved throughput (uploading parts in parallel) and quick recovery from
  26. network issues (only the failed parts need to be retried). The downside is
  27. request overhead, as it needs to do creation, signing (unless you are [signing
  28. on the client][]), and completion requests besides the upload requests. For example,
  29. if you are uploading files that are only a couple kilobytes with a 100ms roundtrip
  30. latency, you are spending 400ms on overhead and only a few milliseconds on uploading.
  31. **In short**
  32. - We recommend the default value of [`shouldUseMultipart`][], which enable
  33. multipart uploads only for large files.
  34. - If you prefer to have less overhead (+20% upload speed) you can use temporary
  35. S3 credentials with [`getTemporarySecurityCredentials`][]. This means users
  36. get a single token which allows them to do bucket operations for longer,
  37. instead of short lived signed URL per resource. This is a security trade-off.
  38. ## Install
  39. <Tabs>
  40. <TabItem value="npm" label="NPM" default>
  41. ```shell
  42. npm install @uppy/aws-s3
  43. ```
  44. </TabItem>
  45. <TabItem value="yarn" label="Yarn">
  46. ```shell
  47. yarn add @uppy/aws-s3
  48. ```
  49. </TabItem>
  50. <TabItem value="cdn" label="CDN">
  51. <UppyCdnExample>
  52. {`
  53. import { Uppy, AwsS3 } from "{{UPPY_JS_URL}}"
  54. new Uppy().use(AwsS3, { /* see options */ })
  55. `}
  56. </UppyCdnExample>
  57. </TabItem>
  58. </Tabs>
  59. ## Use
  60. ### Setting up your S3 bucket
  61. To use this plugin with S3 we need to setup a bucket with the right permissions
  62. and CORS settings.
  63. S3 buckets do not allow public uploads for security reasons. To allow Uppy and
  64. the browser to upload directly to a bucket, its CORS permissions need to be
  65. configured.
  66. CORS permissions can be found in the
  67. [S3 Management Console](https://console.aws.amazon.com/s3/home). Click the
  68. bucket that will receive the uploads, then go into the `Permissions` tab and
  69. select the `CORS configuration` button. A JSON document will be shown that
  70. defines the CORS configuration. (AWS used to use XML but now only allow JSON).
  71. More information about the
  72. [S3 CORS format here](https://docs.amazonaws.cn/en_us/AmazonS3/latest/userguide/ManageCorsUsing.html).
  73. The configuration required for Uppy and Companion is this:
  74. ```json
  75. [
  76. {
  77. "AllowedOrigins": ["https://my-app.com"],
  78. "AllowedMethods": ["GET", "PUT", "POST"],
  79. "MaxAgeSeconds": 3000,
  80. "AllowedHeaders": [
  81. "Authorization",
  82. "x-amz-date",
  83. "x-amz-content-sha256",
  84. "content-type"
  85. ],
  86. "ExposeHeaders": ["ETag", "Location"]
  87. },
  88. {
  89. "AllowedOrigins": ["*"],
  90. "AllowedMethods": ["GET"],
  91. "MaxAgeSeconds": 3000
  92. }
  93. ]
  94. ```
  95. A good practice is to use two CORS rules: one for viewing the uploaded files,
  96. and one for uploading files. This is done above where the first object in the
  97. array defines the rules for uploading, and the second for viewing. The example
  98. above **makes files publicly viewable**. You can change it according to your
  99. needs.
  100. If you are using an IAM policy to allow access to the S3 bucket, the policy must
  101. have at least the `s3:PutObject` and `s3:PutObjectAcl` permissions scoped to the
  102. bucket in question. In-depth documentation about CORS rules is available on the
  103. [AWS documentation site](https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html).
  104. ### Use with your own server
  105. The recommended approach is to integrate `@uppy/aws-s3` with your own server.
  106. You will need to do the following things:
  107. 1. [Setup a S3 bucket](#setting-up-your-s3-bucket).
  108. 2. [Setup your server](https://github.com/transloadit/uppy/blob/main/examples/aws-nodejs/index.js)
  109. 3. [Setup Uppy client](https://github.com/transloadit/uppy/blob/main/examples/aws-nodejs/public/index.html).
  110. ### Use with Companion
  111. [Companion](/docs/companion) has S3 routes built-in for a plug-and-play
  112. experience with Uppy.
  113. :::caution
  114. Generally it’s better for access control, observability, and scaling to
  115. integrate `@uppy/aws-s3` with your own server. You may want to use
  116. [Companion](/docs/companion) for creating, signing, and completing your S3
  117. uploads if you already need Companion for remote files (such as from Google
  118. Drive). Otherwise it’s not worth the hosting effort.
  119. :::
  120. ```js {10} showLineNumbers
  121. import Uppy from '@uppy/core';
  122. import Dashboard from '@uppy/dashboard';
  123. import AwsS3 from '@uppy/aws-s3';
  124. import '@uppy/core/dist/style.min.css';
  125. import '@uppy/dashboard/dist/style.min.css';
  126. const uppy = new Uppy()
  127. .use(Dashboard, { inline: true, target: 'body' })
  128. .use(AwsS3, {
  129. endpoint: 'https://companion.uppy.io',
  130. });
  131. ```
  132. ### Use with TypeScript
  133. Uppy always puts the response to an upload in `file.response.body`. If you want
  134. this to be type safe with `@uppy/aws-s3`, you can import the `AwsBody` type and
  135. pass it as the second genric to `Uppy`.
  136. ```ts {3,12} showLineNumbers
  137. import Uppy from '@uppy/core';
  138. import Dashboard from '@uppy/dashboard';
  139. import AwsS3, { type AwsBody } from '@uppy/aws-s3';
  140. import '@uppy/core/dist/style.min.css';
  141. import '@uppy/dashboard/dist/style.min.css';
  142. // Set this to `any` or `Record<string, unknown>`
  143. // if you do not set any metadata yourself
  144. type Meta = { license: string };
  145. const uppy = new Uppy<Meta, AwsBody>()
  146. .use(Dashboard, { inline: true, target: 'body' })
  147. .use(AwsS3, { endpoint: '...' });
  148. const id = uppy.addFile(/* ... */);
  149. await uppy.upload();
  150. const body = uppy.getFile(id).response.body!;
  151. const { location } = body; // This is now type safe
  152. ```
  153. ## API
  154. ### Options
  155. #### `shouldUseMultipart(file)`
  156. A boolean, or a function that returns a boolean which is called for each file
  157. that is uploaded with the corresponding `UppyFile` instance as argument.
  158. By default, all files with a `file.size` ≤ 100&nbsp;MiB will be uploaded in a
  159. single chunk, all files larger than that as multipart.
  160. Here’s how to use it:
  161. ```js
  162. uppy.use(AwsS3, {
  163. shouldUseMultipart(file) {
  164. // Use multipart only for files larger than 100MiB.
  165. return file.size > 100 * 2 ** 20;
  166. },
  167. });
  168. ```
  169. #### `limit`
  170. The maximum amount of files to upload in parallel (`number`, default: `6`).
  171. Note that the amount of files is not the same as the amount of concurrent
  172. connections. Multipart uploads can use many requests per file. For example, for
  173. a 100 MiB file with a part size of 5 MiB:
  174. - 1 `createMultipartUpload` request
  175. - 100/5 = 20 sign requests (unless you are [signing on the client][])
  176. - 100/5 = 20 upload requests
  177. - 1 `completeMultipartUpload` request
  178. :::caution
  179. Unless you have a good reason and are well informed about the average internet
  180. speed of your users, do not set this higher. S3 uses HTTP/1.1, which means a
  181. limit to concurrent connections and your uploads may expire before they are
  182. uploaded.
  183. :::
  184. #### `endpoint`
  185. URL to your backend or to [Companion](/docs/companion) (`string`, default:
  186. `null`).
  187. #### `headers`
  188. Custom headers that should be sent along to the [`endpoint`](#endpoint) on every
  189. request (`Object`, default: `{}`).
  190. #### `cookiesRule`
  191. This option correlates to the
  192. [RequestCredentials value](https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials)
  193. (`string`, default: `'same-origin'`).
  194. This tells the plugin whether to send cookies to the [`endpoint`](#endpoint).
  195. #### `retryDelays`
  196. `retryDelays` are the intervals in milliseconds used to retry a failed chunk
  197. (`array`, default: `[0, 1000, 3000, 5000]`).
  198. This is also used for [`signPart()`](#signpartfile-partdata). Set to `null` to
  199. disable automatic retries, and fail instantly if any chunk fails to upload.
  200. #### `getChunkSize(file)`
  201. A function that returns the minimum chunk size to use when uploading the given
  202. file as multipart.
  203. For multipart uploads, chunks are sent in batches to have presigned URLs
  204. generated with [`signPart()`](#signpartfile-partdata). To reduce the amount of
  205. requests for large files, you can choose a larger chunk size, at the cost of
  206. having to re-upload more data if one chunk fails to upload.
  207. S3 requires a minimum chunk size of 5MiB, and supports at most 10,000 chunks per
  208. multipart upload. If `getChunkSize()` returns a size that’s too small, Uppy will
  209. increase it to S3’s minimum requirements.
  210. #### `getUploadParameters(file, options)`
  211. :::note
  212. When using [Companion][companion docs] to sign S3 uploads, you should not define
  213. this option.
  214. :::
  215. A function that will be called for each non-multipart upload.
  216. - `file`: `UppyFile` the file that will be uploaded
  217. - `options`: `object`
  218. - `signal`: `AbortSignal`
  219. - **Returns:** `object | Promise<object>`
  220. - `method`: `string`, the HTTP method to be used for the upload. This should
  221. be one of either `PUT` or `POST`, depending on the type of upload used.
  222. - `url`: `string`, the URL to which the upload request will be sent. When
  223. using a presigned PUT upload, this should be the URL to the S3 object with
  224. signing parameters included in the query string. When using a POST upload
  225. with a policy document, this should be the root URL of the bucket.
  226. - `fields` `object`, an object with form fields to send along with the upload
  227. request. For presigned PUT uploads (which are default), this should be left
  228. empty.
  229. - `headers`: `object`, an object with request headers to send along with the
  230. upload request. When using a presigned PUT upload, it’s a good idea to
  231. provide `headers['content-type']`. That will make sure that the request uses
  232. the same content-type that was used to generate the signature. Without it,
  233. the browser may decide on a different content-type instead, causing S3 to
  234. reject the upload.
  235. #### `createMultipartUpload(file)`
  236. A function that calls the S3 Multipart API to create a new upload.
  237. `file` is the file object from Uppy’s state. The most relevant keys are
  238. `file.name` and `file.type`.
  239. Return a Promise for an object with keys:
  240. - `uploadId` - The UploadID returned by S3.
  241. - `key` - The object key for the file. This needs to be returned to allow it to
  242. be different from the `file.name`.
  243. The default implementation calls out to Companion’s S3 signing endpoints.
  244. #### `listParts(file, { uploadId, key })`
  245. A function that calls the S3 Multipart API to list the parts of a file that have
  246. already been uploaded.
  247. Receives the `file` object from Uppy’s state, and an object with keys:
  248. - `uploadId` - The UploadID of this Multipart upload.
  249. - `key` - The object key of this Multipart upload.
  250. Return a Promise for an array of S3 Part objects, as returned by the S3
  251. Multipart API. Each object has keys:
  252. - `PartNumber` - The index in the file of the uploaded part.
  253. - `Size` - The size of the part in bytes.
  254. - `ETag` - The ETag of the part, used to identify it when completing the
  255. multipart upload and combining all parts into a single file.
  256. The default implementation calls out to Companion’s S3 signing endpoints.
  257. #### `signPart(file, partData)`
  258. A function that generates a signed URL for the specified part number. The
  259. `partData` argument is an object with the keys:
  260. - `uploadId` - The UploadID of this Multipart upload.
  261. - `key` - The object key in the S3 bucket.
  262. - `partNumber` - can’t be zero.
  263. - `body` – The data that will be signed.
  264. - `signal` – An `AbortSignal` that may be used to abort an ongoing request.
  265. This function should return a object, or a promise that resolves to an object,
  266. with the following keys:
  267. - `url` – the presigned URL, as a `string`.
  268. - `headers` – **(Optional)** Custom headers to send along with the request to S3
  269. endpoint.
  270. An example of what the return value should look like:
  271. ```json
  272. {
  273. "url": "https://bucket.region.amazonaws.com/path/to/file.jpg?partNumber=1&...",
  274. "headers": { "Content-MD5": "foo" }
  275. }
  276. ```
  277. #### `abortMultipartUpload(file, { uploadId, key })`
  278. A function that calls the S3 Multipart API to abort a Multipart upload, and
  279. removes all parts that have been uploaded so far.
  280. Receives the `file` object from Uppy’s state, and an object with keys:
  281. - `uploadId` - The UploadID of this Multipart upload.
  282. - `key` - The object key of this Multipart upload.
  283. This is typically called when the user cancels an upload. Cancellation cannot
  284. fail in Uppy, so the result of this function is ignored.
  285. The default implementation calls out to Companion’s S3 signing endpoints.
  286. #### `completeMultipartUpload(file, { uploadId, key, parts })`
  287. A function that calls the S3 Multipart API to complete a Multipart upload,
  288. combining all parts into a single object in the S3 bucket.
  289. Receives the `file` object from Uppy’s state, and an object with keys:
  290. - `uploadId` - The UploadID of this Multipart upload.
  291. - `key` - The object key of this Multipart upload.
  292. - `parts` - S3-style list of parts, an array of objects with `ETag` and
  293. `PartNumber` properties. This can be passed straight to S3’s Multipart API.
  294. Return a Promise for an object with properties:
  295. - `location` - **(Optional)** A publicly accessible URL to the object in the S3
  296. bucket.
  297. The default implementation calls out to Companion’s S3 signing endpoints.
  298. #### `allowedMetaFields: null`
  299. Pass an array of field names to limit the metadata fields that will be added to
  300. upload as query parameters.
  301. - Set it to `false` to not send any fields (or an empty array).
  302. - Set it to `['name']` to only send the `name` field.
  303. - Set it to `true` (the default) to send _all_ metadata fields.
  304. #### `getTemporarySecurityCredentials(options)`
  305. :::note
  306. When using [Companion][companion docs] as a backend, you can pass `true` instead
  307. of a function. Setting up Companion will not simplify the process of getting
  308. signing on the client.
  309. :::
  310. A boolean (when using Companion), or an (async) function to retrieve temporary
  311. security credentials used for all uploads instead of signing every part. This
  312. results in less request overhead which can lead to around 20% faster uploads.
  313. This is a security tradeoff. We recommend to not use this option unless you are
  314. familiar with the security implications of temporary credentials, and how to
  315. setup your bucket to make it work. See the
  316. [Requesting temporary security credentials](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_request.html)
  317. AWS guide for more information.
  318. It’s strongly recommended to have some sort of caching process to avoid
  319. requesting more temporary token than necessary.
  320. - `options`: `object`
  321. - `signal`: `AbortSignal`
  322. - **Returns:** `object | Promise<object>`
  323. - `credentials`: `object`
  324. - `AccessKeyId`: `string`
  325. - `SecretAccessKey`: `string`
  326. - `SessionToken`: `string`
  327. - `Expiration`: `string`
  328. - `bucket`: `string`
  329. - `region`: `string`
  330. If you are using Companion (for example because you want to support remote
  331. upload sources), you can pass a boolean:
  332. ```js
  333. uppy.use(AwsS3, {
  334. // This is an example using Companion:
  335. endpoint: 'http://companion.uppy.io',
  336. getTemporarySecurityCredentials: true,
  337. shouldUseMultipart: (file) => file.size > 100 * 2 ** 20,
  338. });
  339. ```
  340. In the most common case, you are using a different backend, in which case you
  341. need to specify a function:
  342. ```js
  343. uppy.use(AwsS3, {
  344. // This is an example not using Companion:
  345. async getTemporarySecurityCredentials({ signal }) {
  346. const response = await fetch('/sts-token', { signal });
  347. if (!response.ok)
  348. throw new Error('Failed to fetch STS', { cause: response });
  349. return response.json();
  350. },
  351. shouldUseMultipart: (file) => file.size > 100 * 2 ** 20,
  352. });
  353. ```
  354. [`gettemporarysecuritycredentials`]: #gettemporarysecuritycredentialsoptions
  355. [`shouldusemultipart`]: #shouldusemultipartfile
  356. [companion docs]: /docs/companion
  357. [signing on the client]: #gettemporarysecuritycredentialsoptions