What is @aws-sdk/middleware-endpoint?
The @aws-sdk/middleware-endpoint package is part of the AWS SDK for JavaScript v3. It is used to manage and resolve endpoints in AWS service calls. This middleware allows developers to customize endpoint resolution logic, which can be useful for directing requests to specific regions, using custom domain names, or working with mock endpoints for testing purposes.
Custom Endpoint Resolution
This code demonstrates how to use the @aws-sdk/middleware-endpoint package to customize the endpoint for an S3 client. It adds a middleware that resolves to a custom endpoint, which is useful for directing requests to a specific server or for testing with mock endpoints.
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');
const { endpointMiddleware } = require('@aws-sdk/middleware-endpoint');
const client = new S3Client({
region: 'us-west-2'
});
client.middlewareStack.add(endpointMiddleware({
endpoint: () => ({
protocol: 'https:',
hostname: 'my-custom-s3-endpoint.com',
path: '/',
query: {}
})
}), {
step: 'build'
});
const command = new GetObjectCommand({ Bucket: 'example-bucket', Key: 'example-object' });
client.send(command);