Skip to content

Commit 5126680

Browse files
committed
ack pr comments
1 parent bc9b4a2 commit 5126680

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

apps/sim/tools/s3/utils.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export function parseS3Uri(
3232
const url = new URL(s3Uri)
3333
const hostname = url.hostname
3434
const normalizedPath = url.pathname.startsWith('/') ? url.pathname.slice(1) : url.pathname
35-
const pathSegments = normalizedPath.split('/').filter(Boolean)
3635

3736
const virtualHostedDualstackMatch = hostname.match(
3837
/^(.+)\.s3\.dualstack\.([^.]+)\.amazonaws\.com(?:\.cn)?$/
@@ -52,14 +51,20 @@ export function parseS3Uri(
5251
pathStyleDualstackMatch || pathStyleRegionalMatch || pathStyleGlobalMatch
5352
)
5453

54+
const firstSlashIndex = normalizedPath.indexOf('/')
55+
const pathStyleBucketName =
56+
firstSlashIndex === -1 ? normalizedPath : normalizedPath.slice(0, firstSlashIndex)
57+
const pathStyleObjectKey =
58+
firstSlashIndex === -1 ? '' : normalizedPath.slice(firstSlashIndex + 1)
59+
5560
const bucketName = isPathStyleHost
56-
? pathSegments[0]
61+
? pathStyleBucketName
5762
: (virtualHostedDualstackMatch?.[1] ??
5863
virtualHostedRegionalMatch?.[1] ??
5964
virtualHostedGlobalMatch?.[1] ??
6065
'')
6166

62-
const rawObjectKey = isPathStyleHost ? pathSegments.slice(1).join('/') : normalizedPath
67+
const rawObjectKey = isPathStyleHost ? pathStyleObjectKey : normalizedPath
6368
const objectKey = (() => {
6469
try {
6570
return decodeURIComponent(rawObjectKey)
@@ -68,13 +73,13 @@ export function parseS3Uri(
6873
}
6974
})()
7075

71-
const region =
76+
const normalizedFallbackRegion = fallbackRegion?.trim()
77+
const regionFromHost =
7278
virtualHostedDualstackMatch?.[2] ??
7379
virtualHostedRegionalMatch?.[2] ??
7480
pathStyleDualstackMatch?.[1] ??
75-
pathStyleRegionalMatch?.[1] ??
76-
fallbackRegion ??
77-
'us-east-1'
81+
pathStyleRegionalMatch?.[1]
82+
const region = regionFromHost || normalizedFallbackRegion || 'us-east-1'
7883

7984
if (!bucketName || !objectKey) {
8085
throw new Error('Invalid S3 URI format')

0 commit comments

Comments
 (0)