s3-client.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const S3 = require('aws-sdk/clients/s3')
  2. const AWS = require('aws-sdk')
  3. /**
  4. * instantiates the aws-sdk s3 client that will be used for s3 uploads.
  5. *
  6. * @param {object} companionOptions the companion options object
  7. */
  8. module.exports = (companionOptions) => {
  9. let s3Client = null
  10. if (companionOptions.s3) {
  11. const { s3 } = companionOptions
  12. if (s3.accessKeyId || s3.secretAccessKey) {
  13. 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`.')
  14. }
  15. const rawClientOptions = s3.awsClientOptions
  16. if (rawClientOptions && (rawClientOptions.accessKeyId || rawClientOptions.secretAccessKey)) {
  17. 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.')
  18. }
  19. const s3ClientOptions = {
  20. signatureVersion: 'v4',
  21. endpoint: s3.endpoint,
  22. region: s3.region,
  23. // backwards compat
  24. useAccelerateEndpoint: s3.useAccelerateEndpoint,
  25. ...rawClientOptions,
  26. }
  27. // Use credentials to allow assumed roles to pass STS sessions in.
  28. // If the user doesn't specify key and secret, the default credentials (process-env)
  29. // will be used by S3 in calls below.
  30. if (s3.key && s3.secret && !s3ClientOptions.credentials) {
  31. s3ClientOptions.credentials = new AWS.Credentials(
  32. s3.key,
  33. s3.secret,
  34. s3.sessionToken,
  35. )
  36. }
  37. s3Client = new S3(s3ClientOptions)
  38. }
  39. return s3Client
  40. }