Skip to content

Commit 65d6180

Browse files
committed
Include the path length when substring-ing the url when using forcePathStyle
1 parent 21bf825 commit 65d6180

File tree

1 file changed

+15
-0
lines changed
  • api/server/services/Files/S3

1 file changed

+15
-0
lines changed

api/server/services/Files/S3/crud.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ const {
1111
DeleteObjectCommand,
1212
} = require('@aws-sdk/client-s3');
1313

14+
const endpoint = process.env.AWS_ENDPOINT_URL;
1415
const bucketName = process.env.AWS_BUCKET_NAME;
1516
const defaultBasePath = 'images';
17+
const forcePathStyle = ['1', 'true', 'yes'].includes(process.env.AWS_FORCE_PATH_STYLE?.toLowerCase());
1618

1719
let s3UrlExpirySeconds = 2 * 60; // 2 minutes
1820
let s3RefreshExpiryMs = null;
@@ -250,6 +252,13 @@ function extractKeyFromS3Url(fileUrlOrKey) {
250252

251253
try {
252254
const url = new URL(fileUrlOrKey);
255+
256+
if (endpoint?.trim() && forcePathStyle) {
257+
const endpointUrl = new URL(endpoint)
258+
const startPos = endpointUrl.pathname.length + (endpointUrl.pathname.endsWith('/') ? 2 : 1) + bucketName.length + 1;
259+
return url.pathname.substring(startPos);
260+
}
261+
253262
return url.pathname.substring(1);
254263
} catch (error) {
255264
const parts = fileUrlOrKey.split('/');
@@ -258,6 +267,12 @@ function extractKeyFromS3Url(fileUrlOrKey) {
258267
return fileUrlOrKey;
259268
}
260269

270+
if (endpoint?.trim() && forcePathStyle) {
271+
const endpointUrl = new URL(endpoint)
272+
const startPos = endpointUrl.pathname.length + (endpointUrl.pathname.endsWith('/') ? 2 : 1) + bucketName.length + 1;
273+
return fileUrlOrKey.substring(startPos);
274+
}
275+
261276
return fileUrlOrKey.startsWith('/') ? fileUrlOrKey.substring(1) : fileUrlOrKey;
262277
}
263278
}

0 commit comments

Comments
 (0)