@@ -11,8 +11,10 @@ const {
1111 DeleteObjectCommand,
1212} = require ( '@aws-sdk/client-s3' ) ;
1313
14+ const endpoint = process . env . AWS_ENDPOINT_URL ;
1415const bucketName = process . env . AWS_BUCKET_NAME ;
1516const defaultBasePath = 'images' ;
17+ const forcePathStyle = [ '1' , 'true' , 'yes' ] . includes ( process . env . AWS_FORCE_PATH_STYLE ?. toLowerCase ( ) ) ;
1618
1719let s3UrlExpirySeconds = 2 * 60 ; // 2 minutes
1820let 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