s3-client.js 2.0 KB

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