Browse Source

feat(s3-multipart): add optional headers for signed url (#1985)

Update `prepareUploadPart` contract to accept an optional `headers` property in the returned value.
If `headers` are present, they will be sent along the S3 presigned URL

Fixes #1984
Corentin Ardeois 5 years ago
parent
commit
178343d264

+ 8 - 3
packages/@uppy/aws-s3-multipart/src/MultipartUploader.js

@@ -160,8 +160,8 @@ class MultipartUploader {
         throw new TypeError('AwsS3/Multipart: Got incorrect result from `prepareUploadPart()`, expected an object `{ url }`.')
       }
       return result
-    }).then(({ url }) => {
-      this._uploadPartBytes(index, url)
+    }).then(({ url, headers }) => {
+      this._uploadPartBytes(index, url, headers)
     }, (err) => {
       this._onError(err)
     })
@@ -189,10 +189,15 @@ class MultipartUploader {
     this._uploadParts()
   }
 
-  _uploadPartBytes (index, url) {
+  _uploadPartBytes (index, url, headers) {
     const body = this.chunks[index]
     const xhr = new XMLHttpRequest()
     xhr.open('PUT', url, true)
+    if (headers) {
+      Object.keys(headers).map((key) => {
+        xhr.setRequestHeader(key, headers[key])
+      })
+    }
     xhr.responseType = 'text'
 
     this.uploading.push(xhr)

+ 1 - 1
packages/@uppy/aws-s3-multipart/types/index.d.ts

@@ -11,7 +11,7 @@ declare module AwsS3Multipart {
     companionUrl: string;
     createMultipartUpload(file: Uppy.UppyFile): Promise<{ uploadId: string, key: string }>;
     listParts(file: Uppy.UppyFile, opts: { uploadId: string, key: string }): Promise<AwsS3Part[]>;
-    prepareUploadPart(file: Uppy.UppyFile, partData: { uploadId: string, key: string, body: Blob, number: number }): Promise<{ url: string }>;
+    prepareUploadPart(file: Uppy.UppyFile, partData: { uploadId: string, key: string, body: Blob, number: number }): Promise<{ url: string, headers?: {[k: string]: string}}>;
     abortMultipartUpload(file: Uppy.UppyFile, opts: { uploadId: string, key: string }): Promise<void>;
     completeMultipartUpload(file: Uppy.UppyFile, opts: { uploadId: string, key: string, parts: AwsS3Part[] }): Promise<{ location?: string }>;
     timeout: number;

+ 1 - 0
website/src/docs/aws-s3-multipart.md

@@ -103,6 +103,7 @@ Return a Promise for an object with keys:
      Expires: 5 * 60,
    }, (err, url) => { /* there's the url! */ })
    ```
+ - `headers` - **(Optional)** Custom headers that should be sent to the S3 presigned URL.
 
 ### abortMultipartUpload(file, { uploadId, key })