Skip to content

Commit fa229d0

Browse files
gregnrcarlopi
authored andcommitted
refactor: simplify s3 path fixes
1 parent 86baaf8 commit fa229d0

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

packages/duckdb-wasm/src/utils/s3_helper.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,9 @@ export interface S3PayloadParams {
2020
contentType: string | null;
2121
}
2222

23-
const getEndpointPathPrefix = function (config: S3Config | undefined): string {
24-
if (config?.endpoint?.startsWith('http')) {
25-
const endpointUrl = new URL(config.endpoint);
26-
// Return the path prefix if present, otherwise empty string
27-
return endpointUrl.pathname !== '/' ? endpointUrl.pathname : '';
28-
}
29-
return '';
30-
};
31-
3223
const getHTTPHost = function (config: S3Config | undefined, url: string, bucket: string): string {
3324
if (config?.endpoint?.startsWith('http')) {
34-
// Endpoint is a full url, extract just the hostname (no path)
25+
// Endpoint is a full url, extract just the host (no path)
3526
const endpointUrl = new URL(config.endpoint);
3627
return endpointUrl.host;
3728
} else if (config?.endpoint) {
@@ -46,14 +37,20 @@ const getHTTPHost = function (config: S3Config | undefined, url: string, bucket:
4637
export function getS3Params(config: S3Config | undefined, url: string, method: string): S3Params {
4738
const parsedS3Url = parseS3Url(url);
4839

49-
// when using S3 path-style access, the signed URL should also include the bucket name,
50-
// as it is present in the HTTP URL path.
40+
// when using S3 path-style access, the signed URL should also include the endpoint's path + bucket name,
41+
// as they will both be present in the HTTP URL path.
5142
// See: https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html#path-style-url-ex
5243
let path = parsedS3Url.path;
5344
if (isPathStyleAccess(config)) {
54-
// Extract endpoint path prefix if present (e.g., "/some/path/prefix" from "https://host/some/path/prefix")
55-
const endpointPathPrefix = getEndpointPathPrefix(config);
56-
path = `${endpointPathPrefix}/${parsedS3Url.bucket}${path}`;
45+
// Extract endpoint path if present (e.g., "/some/path" from "https://host/some/path")
46+
let endpointPath = '';
47+
if (config?.endpoint) {
48+
const endpointUrl = new URL(config.endpoint);
49+
if (endpointUrl.pathname !== '/') {
50+
endpointPath = endpointUrl.pathname;
51+
}
52+
}
53+
path = `${endpointPath}/${parsedS3Url.bucket}${path}`;
5754
}
5855
return {
5956
url: path,

0 commit comments

Comments
 (0)