@aws- sdk/ util- 创建- 请求
此软件包提供了从给定客户端和命令创建请求对象的功能. 您可以提供节点客户端或浏览器客户端. 常见的用例可以是 生成请求对象,然后向预签名者提供创建预签名的url.
当呼叫 createRequest(),则 initialize 财务报告和审定财务报表 serialize 中间软件
从客户端和命令端取出并溶解为处理器。 这个处理器
将返回一个承诺 HttpRequest 对象。 所以任何修改都发生在 build
财务报告和审定财务报表 finalize 中间软件不会被反射生成 httpRequest 对象。 对于
实例 Content-Length 标题不会包含在结果中.
导入 :
//JavaScript:
const createRequest = require("@aws-sdk/util-create-request").createRequest;
//TypeScript:
import { createRequest } from "@aws-sdk/util-create-request";
JavaScript 使用示例 :
const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3");
const request = await createRequest(
new S3Client({}),
new GetObjectCommand({
Bucket: "bucket",
Key: "key",
})
);
/**
{
protocol: 'https:',
path: '/js-sdk-test-bucket/key',
hostname: 's3.us-east-2.amazonaws.com',
body: null,
headers: {},
method: 'GET',
query: {}
}
*/
TypeScript 使用示例 :
import { S3Client } from "@aws-sdk/client-s3";
import { GetObjectCommand } from "@aws-sdk/client-s3";
import { GetObjectCommandInput, GetObjectCommandOutput } from "@aws-sdk/client-s3";
import { Readable } from "stream";
const request = await createRequest<any, GetObjectCommandInput, GetObjectCommandOutput>(
new S3Client({}),
new GetObjectCommand({
Bucket: "bucket",
Key: "key",
})
);
您可以在此函数中省略通则并依赖类型推论. 在这 方式,你会失去类型安全 用于保证客户端,命令来自相同的 服务.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { GetObjectCommand } from "@aws-sdk/client-s3";
/*THIS IS WRONG, but TypeScript won't tell you*/
const request = await createRequest(
new DynamoDBClient({}),
new GetObjectCommand({
Bucket: "bucket",
Key: "key",
})
);