s3-client.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const { S3Client } = require('@aws-sdk/client-s3')
  2. /**
  3. * instantiates the aws-sdk s3 client that will be used for s3 uploads.
  4. *
  5. * @param {object} companionOptions the companion options object
  6. * @param {boolean} createPresignedPostMode whether this s3 client is for createPresignedPost
  7. */
  8. module.exports = (companionOptions, createPresignedPostMode = false) => {
  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. let { endpoint } = s3
  20. if (typeof endpoint === 'string') {
  21. // TODO: deprecate those replacements in favor of what AWS SDK supports out of the box.
  22. endpoint = endpoint.replace(/{service}/, 's3').replace(/{region}/, s3.region)
  23. }
  24. /** @type {import('@aws-sdk/client-s3').S3ClientConfig} */
  25. let s3ClientOptions = {
  26. region: s3.region,
  27. forcePathStyle: Boolean(s3.forcePathStyle)
  28. }
  29. if (s3.useAccelerateEndpoint) {
  30. // https://github.com/transloadit/uppy/issues/4809#issuecomment-1847320742
  31. if (createPresignedPostMode) {
  32. if (s3.bucket != null) {
  33. s3ClientOptions = {
  34. ...s3ClientOptions,
  35. useAccelerateEndpoint: true,
  36. // This is a workaround for lacking support for useAccelerateEndpoint in createPresignedPost
  37. // See https://github.com/transloadit/uppy/issues/4135#issuecomment-1276450023
  38. endpoint: `https://${s3.bucket}.s3-accelerate.amazonaws.com/`,
  39. }
  40. }
  41. } else { // normal useAccelerateEndpoint mode
  42. s3ClientOptions = {
  43. ...s3ClientOptions,
  44. useAccelerateEndpoint: true,
  45. }
  46. }
  47. } else { // no accelearate, we allow custom s3 endpoint
  48. s3ClientOptions = {
  49. ...s3ClientOptions,
  50. endpoint,
  51. }
  52. }
  53. s3ClientOptions = {
  54. ...s3ClientOptions,
  55. ...rawClientOptions,
  56. }
  57. // Use credentials to allow assumed roles to pass STS sessions in.
  58. // If the user doesn't specify key and secret, the default credentials (process-env)
  59. // will be used by S3 in calls below.
  60. if (s3.key && s3.secret && !s3ClientOptions.credentials) {
  61. s3ClientOptions.credentials = {
  62. accessKeyId: s3.key,
  63. secretAccessKey: s3.secret,
  64. sessionToken: s3.sessionToken,
  65. }
  66. }
  67. s3Client = new S3Client(s3ClientOptions)
  68. }
  69. return s3Client
  70. }