aws-s3-multipart.mdx 15 KB

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