Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client-s3): use regional endpoint by default and support aws-glo…
…bal region (#1552) * feat(client-s3): use regional endpoint by default * feat(client-s3): use global endpoint if region is set to aws-global Co-authored-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com>
- Loading branch information
1 parent
0439946
commit 47c6599
Showing
7 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./validate-bucket-name"; | ||
export * from "./use-regional-endpoint"; |
62 changes: 62 additions & 0 deletions
62
packages/middleware-sdk-s3/src/use-regional-endpoint.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { HttpRequest } from "@aws-sdk/protocol-http"; | ||
|
||
import { useRegionalEndpointMiddleware } from "./use-regional-endpoint"; | ||
|
||
describe("useRegionalEndpointMiddleware", () => { | ||
const mockNextHandler = jest.fn(); | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should accept any endpoint if set by customer", async () => { | ||
const config = { isCustomEndpoint: true, region: async () => "foo-region" }; | ||
const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any); | ||
await handler({ | ||
input: {}, | ||
request: new HttpRequest({ | ||
hostname: "s3.us-east-1.amazonaws.com", | ||
}), | ||
}); | ||
expect(mockNextHandler.mock.calls.length).toBe(1); | ||
expect(mockNextHandler.mock.calls[0][0].request.hostname).toEqual("s3.us-east-1.amazonaws.com"); | ||
}); | ||
|
||
it("should modify the hostname if it's global endpoint", async () => { | ||
const config = { isCustomEndpoint: false, region: async () => "foo-region" }; | ||
const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any); | ||
await handler({ | ||
input: {}, | ||
request: new HttpRequest({ | ||
hostname: "s3.amazonaws.com", | ||
}), | ||
}); | ||
expect(mockNextHandler.mock.calls.length).toBe(1); | ||
expect(mockNextHandler.mock.calls[0][0].request.hostname).toEqual("s3.us-east-1.amazonaws.com"); | ||
}); | ||
|
||
it("should not modify the hostname if it's regional endpoint", async () => { | ||
const config = { isCustomEndpoint: false, region: async () => "foo-region" }; | ||
const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any); | ||
await handler({ | ||
input: {}, | ||
request: new HttpRequest({ | ||
hostname: "s3.us-west-2.amazonaws.com", | ||
}), | ||
}); | ||
expect(mockNextHandler.mock.calls.length).toBe(1); | ||
expect(mockNextHandler.mock.calls[0][0].request.hostname).toEqual("s3.us-west-2.amazonaws.com"); | ||
}); | ||
|
||
it("should use global endpoint if region is set to 'aws-global'", async () => { | ||
const config = { isCustomEndpoint: false, region: async () => "aws-global" }; | ||
const handler = useRegionalEndpointMiddleware(config)(mockNextHandler, {} as any); | ||
await handler({ | ||
input: {}, | ||
request: new HttpRequest({ | ||
hostname: "s3.aws-global.amazonaws.com", | ||
}), | ||
}); | ||
expect(mockNextHandler.mock.calls.length).toBe(1); | ||
expect(mockNextHandler.mock.calls[0][0].request.hostname).toEqual("s3.amazonaws.com"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { HttpRequest } from "@aws-sdk/protocol-http"; | ||
import { | ||
BuildHandler, | ||
BuildHandlerArguments, | ||
BuildHandlerOptions, | ||
BuildHandlerOutput, | ||
BuildMiddleware, | ||
MetadataBearer, | ||
Pluggable, | ||
Provider, | ||
} from "@aws-sdk/types"; | ||
|
||
type PreviouslyResolved = { | ||
region: Provider<string>; | ||
isCustomEndpoint: boolean; | ||
}; | ||
|
||
export const useRegionalEndpointMiddleware = (config: PreviouslyResolved): BuildMiddleware<any, any> => < | ||
Output extends MetadataBearer | ||
>( | ||
next: BuildHandler<any, Output> | ||
): BuildHandler<any, Output> => async (args: BuildHandlerArguments<any>): Promise<BuildHandlerOutput<Output>> => { | ||
const { request } = args; | ||
if (!HttpRequest.isInstance(request) || config.isCustomEndpoint) return next({ ...args }); | ||
if (request.hostname === "s3.amazonaws.com") { | ||
request.hostname = "s3.us-east-1.amazonaws.com"; | ||
} else if ("aws-global" === (await config.region())) { | ||
request.hostname = "s3.amazonaws.com"; | ||
} | ||
return next({ ...args }); | ||
}; | ||
|
||
export const useRegionalEndpointMiddlewareOptions: BuildHandlerOptions = { | ||
step: "build", | ||
tags: ["USE_REGIONAL_ENDPOINT", "S3"], | ||
name: "useRegionalEndpointMiddleware", | ||
}; | ||
|
||
export const getUseRegionalEndpointPlugin = (config: PreviouslyResolved): Pluggable<any, any> => ({ | ||
applyToStack: (clientStack) => { | ||
clientStack.add(useRegionalEndpointMiddleware(config), useRegionalEndpointMiddlewareOptions); | ||
}, | ||
}); |