/
s3-client.js
46 lines (40 loc) · 1.75 KB
/
s3-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const S3 = require('aws-sdk/clients/s3')
const AWS = require('aws-sdk')
/**
* instantiates the aws-sdk s3 client that will be used for s3 uploads.
*
* @param {object} companionOptions the companion options object
*/
module.exports = (companionOptions) => {
let s3Client = null
if (companionOptions.s3) {
const { s3 } = companionOptions
if (s3.accessKeyId || s3.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`.')
}
const rawClientOptions = s3.awsClientOptions
if (rawClientOptions && (rawClientOptions.accessKeyId || rawClientOptions.secretAccessKey)) {
throw new Error('Found unsupported `providerOptions.s3.awsClientOptions.accessKeyId` or `providerOptions.s3.awsClientOptions.secretAccessKey` configuration. Please use the `providerOptions.s3.key` and `providerOptions.s3.secret` options instead.')
}
const s3ClientOptions = {
signatureVersion: 'v4',
endpoint: s3.endpoint,
region: s3.region,
// backwards compat
useAccelerateEndpoint: s3.useAccelerateEndpoint,
...rawClientOptions,
}
// 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.
if (s3.key && s3.secret && !s3ClientOptions.credentials) {
s3ClientOptions.credentials = new AWS.Credentials(
s3.key,
s3.secret,
s3.sessionToken,
)
}
s3Client = new S3(s3ClientOptions)
}
return s3Client
}