Skip to content

Commit

Permalink
create a separate s3 client
Browse files Browse the repository at this point in the history
for `createPresignedPost`
fixes #4809
  • Loading branch information
mifi committed Dec 8, 2023
1 parent 1b6c260 commit 9c8c0f0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/@uppy/companion/src/server/controllers/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion packages/@uppy/companion/src/server/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
41 changes: 29 additions & 12 deletions packages/@uppy/companion/src/server/s3-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
}

Expand Down

0 comments on commit 9c8c0f0

Please sign in to comment.