@aws-sdk/config
此套件导出配置 JavaScript v3 的 AWS SDK 的依赖性执行 用于应用程序代码的客户端.
关于SDK客户端配置选项的概述, 见 补充文件/文件。 。 。 .
目录
- 使用量
@aws-sdk/config/credentials@aws-sdk/config/logger@aws-sdk/config/protocol@aws-sdk/config/requestHandler@aws-sdk/config/retryStrategy@aws-sdk/config/typecheck
使用量
与您的 SDK 客户端软件包一起安装此选项以访问可选 SDK 插件和可注入 依赖性执行.
{
name: "your-app",
dependencies: {
"@aws-sdk/client-s3": "<=3.x.y",
// where 3.x.y is a version of your choosing.
"@aws-sdk/client-dynamodb": "<=3.x.y",
"@aws-sdk/config": "<=3.x.y",
},
}
从此包输出的元素被组织成可被导入路径访问的子模块.
@aws-sdk/config/credentials
此模块包括从 @aws-sdk/credential-providers。 。 。 .
// example: credentials
import { fromIni } from "@aws-sdk/config/credentials";
new S3Client({
credentials: fromIni(),
});
见 @aws-sdk/证书提供者 为完整的API.
见 (单位:千美元) 证书。 。 。 .
@aws-sdk/config/logger
此模块会导出一个用于日志级别过滤的助手 .
// example: logger
import { LogLevel } from "@aws-sdk/config/logger";
new S3Client({
logger: new LogLevel("debug", console),
});
见 (原始内容存档于2018-06-21) (英语). CLIENTS.md#logger 执行自己的记录器.
@aws-sdk/config/protocol
此模块导出一组 AWS 协议, 您可以用于协议选择 覆盖或扩展 自定义序列化行为(先进用法).
// example: protocol selection.
import { CloudWatchClient } from "@aws-sdk/client-cloudwatch";
import { AwsSmithyRpcV2CborProtocol } from "@aws-sdk/config/protocol";
new CloudWatchClient({
protocol: AwsSmithyRpcV2CborProtocol,
});
// example: protocol extension.
import { AwsRestXmlProtocol } from "@aws-sdk/config/protocol";
new S3Client({
protocol: class CustomAwsRestXmlProtocol extends AwsRestXmlProtocol {},
});
见 CLIENTS.md# 议定书书。 。 。 .
@aws-sdk/config/requestHandler
此模块导出两个常见的 HTTP 请求处理器执行 用于扩展.
// example: requestHandler extension.
import { NodeHttpHandler, FetchHttpHandler } from "@aws-sdk/config/requestHandler";
new S3Client({
requestHandler: new (class extends NodeHttpHandler {
async handle(request) {
return super.handle(request);
}
})(),
});
见 CLIENTS.md# 请求汉德勒。 。 。 .
@aws-sdk/config/retryStrategy
此模块输出 SDK 重试策略执行 。 请记住
通常,你不需要自定义 SDK的再试验行为
maxAttempts 财务报告和审定财务报表 retryMode 可配置不需要导入的简单值.
见 战略。 。 。 .
// example: retryStrategy with custom backoff.
import { ConfiguredRetryStrategy } from "@aws-sdk/config/retryStrategy";
new S3Client({
retryStrategy: new ConfiguredRetryStrategy(
// attempts
5,
// backoff 1 additional second per attempt
(attempt: number) => 500 + attempt * 1_000
),
});
@aws-sdk/config/typecheck
此模块导出编译时间类型变换帮助器和运行时间类型检查组件 .
编译时间转换
编译时间类型变换帮助工具,请参见: @ smith/ 类型。 。 。 .
// example: type transformers.
import type { AssertiveClient, BrowserClient, NodeJsClient, UncheckedClient } from "@aws-sdk/config/typecheck";
// UncheckedClient makes output fields non-nullable.
// You should still perform type checks as you deem
// necessary, but the SDK will no longer prompt you
// with nullability errors.
const s3 = new S3({}) as UncheckedClient<S3>;
// this transform narrows the type of output streams to Node.js Readable.
const s3Nodejs = new S3Client({}) as NodeJsClient<S3Client>;
// this transform narrows the type of output streams to web ReadableStream.
const s3Browser = new S3Client({}) as BrowserClient<S3Client>;
运行时间类型检查
参见 谢马斯·姆德 用于内导体 到SDK如何使用schemas来序列化和去序列化数据结构. 斯克马斯也是 用于运行时类型检查插件.
// example: runtime typecheck usage with SDK client
import { DynamoDB } from "@aws-sdk/client-dynamodb";
import { getRuntimeTypecheckPlugin } from "@aws-sdk/config/typecheck";
const ddb = new DynamoDB();
ddb.middlewareStack.use(
// requires AWS SDK v3.953.0+ for shape checking.
// requires AWS SDK v3.969.0+ for required member checks.
getRuntimeTypecheckPlugin({
logger: console,
// throw means stop execution,
// other values correspond to logger channel.
input: "throw" || "error" || "warn" || "info" || "debug",
})
);
await ddb
.getItem({
Key: {
id: {
S: new Date("wrong type"),
},
},
})
.catch(console.error);
/*
Error: DynamoDBClient->GetItemCommand input validation:
{}.TableName: is required.
{}.Key["id"].S: expected string, got object.
<... stack trace ...>
*/
// standalone usage
import { GetItemInput$ } from "@aws-sdk/client-dynamodb";
import { validateSchema } from "@aws-sdk/config/typecheck";
const errors = validateSchema(GetItemInput$, {
Key: {
id: {
S: new Date("wrong type"),
},
},
});
console.log(errors.join("\n"));
/*
{}.TableName: is required.
{}.Key["id"].S: expected string, got object.
*/