瀏覽代碼

Merge pull request #1091 from transloadit/chore/aws-s3-server-headers

aws-s3: Use RequestClient, fixes #1088
Renée Kooi 6 年之前
父節點
當前提交
3609862cb9
共有 3 個文件被更改,包括 27 次插入7 次删除
  1. 1 0
      packages/@uppy/aws-s3/package.json
  2. 20 7
      packages/@uppy/aws-s3/src/index.js
  3. 6 0
      website/src/docs/aws-s3.md

+ 1 - 0
packages/@uppy/aws-s3/package.json

@@ -25,6 +25,7 @@
   "dependencies": {
     "@uppy/utils": "0.27.1",
     "@uppy/xhr-upload": "0.27.4",
+    "@uppy/companion-client": "0.27.2",
     "resolve-url": "^0.2.1"
   },
   "devDependencies": {

+ 20 - 7
packages/@uppy/aws-s3/src/index.js

@@ -2,6 +2,7 @@ const resolveUrl = require('resolve-url')
 const { Plugin } = require('@uppy/core')
 const Translator = require('@uppy/utils/lib/Translator')
 const limitPromises = require('@uppy/utils/lib/limitPromises')
+const { RequestClient } = require('@uppy/companion-client')
 const XHRUpload = require('@uppy/xhr-upload')
 
 function isXml (xhr) {
@@ -9,6 +10,15 @@ function isXml (xhr) {
   return typeof contentType === 'string' && contentType.toLowerCase() === 'application/xml'
 }
 
+function assertServerError (res) {
+  if (res && res.error) {
+    const error = new Error(res.message)
+    Object.assign(error, res.error)
+    throw error
+  }
+  return res
+}
+
 module.exports = class AwsS3 extends Plugin {
   constructor (uppy, opts) {
     super(uppy, opts)
@@ -29,13 +39,18 @@ module.exports = class AwsS3 extends Plugin {
       locale: defaultLocale
     }
 
-    this.opts = Object.assign({}, defaultOptions, opts)
-    this.locale = Object.assign({}, defaultLocale, this.opts.locale)
-    this.locale.strings = Object.assign({}, defaultLocale.strings, this.opts.locale.strings)
+    this.opts = { ...defaultOptions, ...opts }
+    this.locale = {
+      ...defaultLocale,
+      ...this.opts.locale,
+      strings: { ...defaultLocale.strings, ...this.opts.locale.strings }
+    }
 
     this.translator = new Translator({ locale: this.locale })
     this.i18n = this.translator.translate.bind(this.translator)
 
+    this.client = new RequestClient(uppy, opts)
+
     this.prepareUpload = this.prepareUpload.bind(this)
 
     if (typeof this.opts.limit === 'number' && this.opts.limit !== 0) {
@@ -52,10 +67,8 @@ module.exports = class AwsS3 extends Plugin {
 
     const filename = encodeURIComponent(file.name)
     const type = encodeURIComponent(file.type)
-    return fetch(`${this.opts.serverUrl}/s3/params?filename=${filename}&type=${type}`, {
-      method: 'get',
-      headers: { accept: 'application/json' }
-    }).then((response) => response.json())
+    return this.client.get(`s3/params?filename=${filename}&type=${type}`)
+      .then(assertServerError)
   }
 
   validateParameters (file, params) {

+ 6 - 0
website/src/docs/aws-s3.md

@@ -58,6 +58,12 @@ uppy.use(AwsS3, {
 })
 ```
 
+### `serverHeaders: {}`
+
+> Note: This only applies when using [Companion][companion docs] to sign S3 uploads.
+
+Custom headers that should be sent along to [Companion][companion docs] on every request.
+
 ### `getUploadParameters(file)`
 
 > Note: When using [Companion][companion docs] to sign S3 uploads, do not define this option.