Skip to content

Commit

Permalink
companion: allow providing any S3 option, closes #1388
Browse files Browse the repository at this point in the history
Now you can configure any S3 option listed in
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property.

It adds a warning about the official S3 names `accessKeyId` and
`secretAccessKey` because you should use the Companion-style names `key`
and `secret` instead.
  • Loading branch information
goto-bus-stop committed Jan 20, 2020
1 parent 7eda234 commit 081d2f8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
32 changes: 20 additions & 12 deletions packages/@uppy/companion/src/companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,25 +212,33 @@ const getOptionsMiddleware = (options) => {
if (options.providerOptions.s3) {
const S3 = require('aws-sdk/clients/s3')
const AWS = require('aws-sdk')
const config = options.providerOptions.s3
const s3ProviderOptions = options.providerOptions.s3
const s3Options = Object.assign({
signatureVersion: 'v4'
}, s3ProviderOptions)
// delete options intended for Companion, not S3:
delete s3Options.key
delete s3Options.secret
delete s3Options.conditions
delete s3Options.getKey

if (s3ProviderOptions.accessKeyId || s3ProviderOptions.secretAccessKey) {
throw new Error('Found `providerOptions.s3.accessKeyId` or `providerOptions.s3.secretAccessKey` configuration, but Companion requires `key` and `secret` option names instead. Please use the `key` property instead of `accessKeyId` and the `secret` property instead of `secretAccessKey`.')
}

// Use credentials to allow assumed roles to pass STS sessions in.
// If the user doesn't specify key and secret, the default credentials (process-env)
// will be used by S3 in calls below.
let credentials
if (config.key && config.secret) {
credentials = new AWS.Credentials(config.key, config.secret, config.sessionToken)
if (s3ProviderOptions.key && s3ProviderOptions.secret && !s3Options.credentials) {
s3Options.credentials = new AWS.Credentials(
s3ProviderOptions.key,
s3ProviderOptions.secret,
s3ProviderOptions.sessionToken)
}
s3Client = new S3({
region: config.region,
endpoint: config.endpoint,
credentials,
signatureVersion: 'v4',
useAccelerateEndpoint: config.useAccelerateEndpoint
})
s3Client = new S3(s3Options)
}

/**
*
* @param {object} req
* @param {object} res
* @param {function} next
Expand Down
19 changes: 13 additions & 6 deletions website/src/docs/companion.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,15 @@ See [env.example.sh](https://github.com/transloadit/uppy/blob/master/env.example

### S3 options

The S3 uploader has some options in addition to the ones necessary for authentication.
You can supply any [S3 option supported by the AWS SDK](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property) in the `providerOptions.s3` object, _except for_ the below:

- `accessKeyId`. Instead, use the `key` option. This is to make configuration names consistent between different Companion features.
- `secretAccessKey`. Instead, use the `secret` option. This is to make configuration names consistent between different Companion features.

Additionally, some Companion-specific options can be configured:

#### `providerOptions.s3.getKey(req, filename, metadata)`

#### `s3.getKey(req, filename, metadata)`
a
Get the key name for a file. The key is the file path to which the file will be uploaded in your bucket. This option should be a function receiving three arguments:
- `req`, the HTTP request, for _regular_ S3 uploads using the `@uppy/aws-s3` plugin. This parameter is _not_ available for multipart uploads using the `@uppy/aws-s3-multipart` plugin;
- `filename`, the original name of the uploaded file;
Expand All @@ -304,9 +309,11 @@ This function should return a string `key`. The `req` parameter can be used to u
```js
app.use(authenticationMiddleware)
app.use(uppy.app({
s3: {
getKey: (req, filename, metadata) => `${req.user.id}/${filename}`,
/* auth options */
providerOptions: {
s3: {
getKey: (req, filename, metadata) => `${req.user.id}/${filename}`,
/* auth options */
}
}
}))
```
Expand Down

0 comments on commit 081d2f8

Please sign in to comment.