Skip to content

Commit bbfec7a

Browse files
committed
feat(client-s3): use global endpoint if region is set to aws-global
1 parent 0f204d2 commit bbfec7a

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

packages/middleware-sdk-s3/src/use-regional-endpoint.spec.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,22 @@ describe("useRegionalEndpointMiddleware", () => {
88
jest.clearAllMocks();
99
});
1010

11+
it("should accept any endpoint if set by customer", async () => {
12+
const config = { isCustomEndpoint: true, region: async () => "foo-region" };
13+
const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any);
14+
await handler({
15+
input: {},
16+
request: new HttpRequest({
17+
hostname: "s3.us-east-1.amazonaws.com",
18+
}),
19+
});
20+
expect(mockNextHandler.mock.calls.length).toBe(1);
21+
expect(mockNextHandler.mock.calls[0][0].request.hostname).toEqual("s3.us-east-1.amazonaws.com");
22+
});
23+
1124
it("should modify the hostname if it's global endpoint", async () => {
12-
const handler = useRegionalEndpointMiddleware()(mockNextHandler, {} as any);
25+
const config = { isCustomEndpoint: false, region: async () => "foo-region" };
26+
const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any);
1327
await handler({
1428
input: {},
1529
request: new HttpRequest({
@@ -21,7 +35,8 @@ describe("useRegionalEndpointMiddleware", () => {
2135
});
2236

2337
it("should not modify the hostname if it's regional endpoint", async () => {
24-
const handler = useRegionalEndpointMiddleware()(mockNextHandler, {} as any);
38+
const config = { isCustomEndpoint: false, region: async () => "foo-region" };
39+
const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any);
2540
await handler({
2641
input: {},
2742
request: new HttpRequest({
@@ -31,4 +46,17 @@ describe("useRegionalEndpointMiddleware", () => {
3146
expect(mockNextHandler.mock.calls.length).toBe(1);
3247
expect(mockNextHandler.mock.calls[0][0].request.hostname).toEqual("s3.us-west-2.amazonaws.com");
3348
});
49+
50+
it("should use global endpoint if region is set to 'aws-global'", async () => {
51+
const config = { isCustomEndpoint: false, region: async () => "aws-global" };
52+
const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any);
53+
await handler({
54+
input: {},
55+
request: new HttpRequest({
56+
hostname: "s3.aws-global.amazonaws.com",
57+
}),
58+
});
59+
expect(mockNextHandler.mock.calls.length).toBe(1);
60+
expect(mockNextHandler.mock.calls[0][0].request.hostname).toEqual("s3.amazonaws.com");
61+
});
3462
});

packages/middleware-sdk-s3/src/use-regional-endpoint.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@ import {
55
BuildHandlerOptions,
66
BuildHandlerOutput,
77
BuildMiddleware,
8-
Endpoint,
98
MetadataBearer,
109
Pluggable,
1110
Provider,
1211
} from "@aws-sdk/types";
1312

14-
export function useRegionalEndpointMiddleware(config: {
13+
type PreviouslyResolved = {
1514
region: Provider<string>;
16-
endpoint: Provider<Endpoint>;
17-
}): BuildMiddleware<any, any> {
15+
isCustomEndpoint: boolean;
16+
};
17+
18+
export function useRegionalEndpointMiddleware(config: PreviouslyResolved): BuildMiddleware<any, any> {
1819
return <Output extends MetadataBearer>(next: BuildHandler<any, Output>): BuildHandler<any, Output> => async (
1920
args: BuildHandlerArguments<any>
2021
): Promise<BuildHandlerOutput<Output>> => {
2122
const { request } = args;
22-
if (HttpRequest.isInstance(request) && request.hostname === "s3.amazonaws.com") {
23+
if (!HttpRequest.isInstance(request) || config.isCustomEndpoint) return next({ ...args });
24+
if (request.hostname === "s3.amazonaws.com") {
2325
request.hostname = "s3.us-east-1.amazonaws.com";
26+
} else if ("aws-global" === (await config.region())) {
27+
request.hostname = "s3.amazonaws.com";
2428
}
2529
return next({ ...args });
2630
};
@@ -32,9 +36,8 @@ export const useRegionalEndpointMiddlewareOptions: BuildHandlerOptions = {
3236
name: "useRegionalEndpointMiddleware",
3337
};
3438

35-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
36-
export const getUseRegionalEndpointPlugin = (unused: any): Pluggable<any, any> => ({
39+
export const getUseRegionalEndpointPlugin = (config: PreviouslyResolved): Pluggable<any, any> => ({
3740
applyToStack: (clientStack) => {
38-
clientStack.add(useRegionalEndpointMiddleware(), useRegionalEndpointMiddlewareOptions);
41+
clientStack.add(useRegionalEndpointMiddleware(config), useRegionalEndpointMiddlewareOptions);
3942
},
4043
});

0 commit comments

Comments
 (0)