4
4
BuildHandlerArguments ,
5
5
BuildHandlerOutput ,
6
6
BuildMiddleware ,
7
+ HandlerExecutionContext ,
7
8
MetadataBearer ,
8
9
Pluggable ,
9
10
RelativeMiddlewareOptions ,
@@ -15,27 +16,50 @@ import { getPseudoRegion } from "./bucketHostnameUtils";
15
16
import { BucketEndpointResolvedConfig } from "./configurations" ;
16
17
17
18
export function bucketEndpointMiddleware ( options : BucketEndpointResolvedConfig ) : BuildMiddleware < any , any > {
18
- return < Output extends MetadataBearer > ( next : BuildHandler < any , Output > ) : BuildHandler < any , Output > => async (
19
- args : BuildHandlerArguments < any >
20
- ) : Promise < BuildHandlerOutput < Output > > => {
21
- const { Bucket : bucketName } = args . input ;
19
+ return < Output extends MetadataBearer > (
20
+ next : BuildHandler < any , Output > ,
21
+ context : HandlerExecutionContext
22
+ ) : BuildHandler < any , Output > => async ( args : BuildHandlerArguments < any > ) : Promise < BuildHandlerOutput < Output > > => {
23
+ const { Bucket : bucketName } = args . input as { Bucket : string } ;
22
24
let replaceBucketInPath = options . bucketEndpoint ;
23
25
const request = args . request ;
24
26
if ( HttpRequest . isInstance ( request ) ) {
25
27
if ( options . bucketEndpoint ) {
26
28
request . hostname = bucketName ;
27
- } else {
28
- const isBucketNameArn = validateArn ( bucketName ) ;
29
+ } else if ( validateArn ( bucketName ) ) {
30
+ const bucketArn = parseArn ( bucketName ) ;
31
+ const clientRegion = getPseudoRegion ( await options . region ( ) ) ;
32
+ const { partition, signingRegion } = ( await options . regionInfoProvider ( clientRegion ) ) || { } ;
33
+ const useArnRegion = await options . useArnRegion ( ) ;
29
34
const { hostname, bucketEndpoint } = bucketHostname ( {
30
- bucketName : isBucketNameArn ? parseArn ( bucketName ) : bucketName ,
35
+ bucketName : bucketArn ,
31
36
baseHostname : request . hostname ,
32
37
accelerateEndpoint : options . useAccelerateEndpoint ,
33
38
dualstackEndpoint : options . useDualstackEndpoint ,
34
39
pathStyleEndpoint : options . forcePathStyle ,
35
40
tlsCompatible : request . protocol === "https:" ,
36
- ...( isBucketNameArn ? await getArnHostnameParameters ( options ) : { } ) ,
41
+ useArnRegion,
42
+ clientPartition : partition ,
43
+ clientSigningRegion : signingRegion ,
37
44
} ) ;
38
45
46
+ // If the request needs to use a region inferred from ARN that different from client region, we need to set
47
+ // them in the handler context so the signer will use them
48
+ if ( useArnRegion && clientRegion !== bucketArn . region ) {
49
+ context [ "signing_region" ] = bucketArn . region ;
50
+ }
51
+
52
+ request . hostname = hostname ;
53
+ replaceBucketInPath = bucketEndpoint ;
54
+ } else {
55
+ const { hostname, bucketEndpoint } = bucketHostname ( {
56
+ bucketName,
57
+ baseHostname : request . hostname ,
58
+ accelerateEndpoint : options . useAccelerateEndpoint ,
59
+ dualstackEndpoint : options . useDualstackEndpoint ,
60
+ pathStyleEndpoint : options . forcePathStyle ,
61
+ tlsCompatible : request . protocol === "https:" ,
62
+ } ) ;
39
63
request . hostname = hostname ;
40
64
replaceBucketInPath = bucketEndpoint ;
41
65
}
@@ -52,16 +76,6 @@ export function bucketEndpointMiddleware(options: BucketEndpointResolvedConfig):
52
76
} ;
53
77
}
54
78
55
- const getArnHostnameParameters = async ( options : BucketEndpointResolvedConfig ) => {
56
- const clientRegion = await options . region ( ) ;
57
- const clientRegionInfo = await options . regionInfoProvider ( getPseudoRegion ( clientRegion ) ) ;
58
- return {
59
- useArnRegion : await options . useArnRegion ( ) ,
60
- clientPartition : clientRegionInfo ?. partition ,
61
- clientSigningRegion : clientRegionInfo ?. signingRegion ,
62
- } ;
63
- } ;
64
-
65
79
export const bucketEndpointMiddlewareOptions : RelativeMiddlewareOptions = {
66
80
tags : [ "BUCKET_ENDPOINT" ] ,
67
81
name : "bucketEndpointMiddleware" ,
0 commit comments