diff --git a/packages/@uppy/companion/src/server/controllers/s3.js b/packages/@uppy/companion/src/server/controllers/s3.js index 629ab5c68f..3a101711e2 100644 --- a/packages/@uppy/companion/src/server/controllers/s3.js +++ b/packages/@uppy/companion/src/server/controllers/s3.js @@ -24,11 +24,11 @@ module.exports = function s3 (config) { throw new TypeError('s3: The `getKey` option must be a function') } - function getS3Client (req, res) { + function getS3Client (req, res, createPresignedPostMode = false) { /** * @type {import('@aws-sdk/client-s3').S3Client} */ - const client = req.companion.s3Client + const client = createPresignedPostMode ? req.companion.s3ClientCreatePresignedPost : req.companion.s3Client if (!client) res.status(400).json({ error: 'This Companion server does not support uploading to S3' }) return client } diff --git a/packages/@uppy/companion/src/server/middlewares.js b/packages/@uppy/companion/src/server/middlewares.js index 73e8d798cb..be7cb0837b 100644 --- a/packages/@uppy/companion/src/server/middlewares.js +++ b/packages/@uppy/companion/src/server/middlewares.js @@ -204,7 +204,8 @@ exports.getCompanionMiddleware = (options) => { const middleware = (req, res, next) => { req.companion = { options, - s3Client: getS3Client(options), + s3Client: getS3Client(options, false), + s3ClientCreatePresignedPost: getS3Client(options, true), authToken: req.header('uppy-auth-token') || req.query.uppyAuthToken, buildURL: getURLBuilder(options), } diff --git a/packages/@uppy/companion/src/server/s3-client.js b/packages/@uppy/companion/src/server/s3-client.js index 4363fb6624..d5b5c48be6 100644 --- a/packages/@uppy/companion/src/server/s3-client.js +++ b/packages/@uppy/companion/src/server/s3-client.js @@ -4,8 +4,9 @@ const { S3Client } = require('@aws-sdk/client-s3') * instantiates the aws-sdk s3 client that will be used for s3 uploads. * * @param {object} companionOptions the companion options object + * @param {boolean} createPresignedPostMode whether this s3 client is for createPresignedPost */ -module.exports = (companionOptions) => { +module.exports = (companionOptions, createPresignedPostMode = false) => { let s3Client = null if (companionOptions.s3) { const { s3 } = companionOptions @@ -19,23 +20,39 @@ module.exports = (companionOptions) => { 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.') } + let { endpoint } = s3 + if (typeof endpoint === 'string') { + // TODO: deprecate those replacements in favor of what AWS SDK supports out of the box. + endpoint = endpoint.replace(/{service}/, 's3').replace(/{region}/, s3.region) + } + + /** @type {import('@aws-sdk/client-s3').S3ClientConfig} */ let s3ClientOptions = { - endpoint: s3.endpoint, region: s3.region, } - if (typeof s3.endpoint === 'string') { - // TODO: deprecate those replacements in favor of what AWS SDK supports out of the box. - s3ClientOptions.endpoint = s3.endpoint.replace(/{service}/, 's3').replace(/{region}/, s3.region) - } - if (s3.useAccelerateEndpoint && s3.bucket != null) { + if (s3.useAccelerateEndpoint) { + // https://github.com/transloadit/uppy/issues/4809#issuecomment-1847320742 + if (createPresignedPostMode) { + if (s3.bucket != null) { + s3ClientOptions = { + ...s3ClientOptions, + useAccelerateEndpoint: true, + // This is a workaround for lacking support for useAccelerateEndpoint in createPresignedPost + // See https://github.com/transloadit/uppy/issues/4135#issuecomment-1276450023 + endpoint: `https://${s3.bucket}.s3-accelerate.amazonaws.com/`, + } + } + } else { // normal useAccelerateEndpoint mode + s3ClientOptions = { + ...s3ClientOptions, + useAccelerateEndpoint: true, + } + } + } else { // no accelearate, we allow custom s3 endpoint s3ClientOptions = { ...s3ClientOptions, - useAccelerateEndpoint: true, - bucketEndpoint: true, - // This is a workaround for lacking support for useAccelerateEndpoint in createPresignedPost - // See https://github.com/transloadit/uppy/issues/4135#issuecomment-1276450023 - endpoint: `https://${s3.bucket}.s3-accelerate.amazonaws.com/`, + endpoint, } }