Amazon SDK for JavaScript V3 API 参考指南详细描述了 Amazon SDK for JavaScript 版本 3 (V3) 的所有API操作。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
有关特定服务客户端的注意事项
Amazon Lambda
Lambda 调用的响应类型在 v2 和 v3 中有所不同。
// v2 import { Lambda } from "@aws-sdk/client-lambda"; import AWS from "aws-sdk"; const lambda = new AWS.Lambda({
REGION
}); const invoke = await lambda.invoke({ FunctionName: "echo", Payload: JSON.stringify({ message: "hello" }), }).promise(); // in v2, Lambda::invoke::Payload is automatically converted to string via a // specific code customization. const payloadIsString = typeof invoke.Payload === "string"; console.log("Invoke response payload type is string:", payloadIsString); const payloadObject = JSON.parse(invoke.Payload); console.log("Invoke response object", payloadObject);
// v3 const lambda = new Lambda({
REGION
}); const invoke = await lambda.invoke({ FunctionName: "echo", Payload: JSON.stringify({ message: "hello" }), }); // in v3, Lambda::invoke::Payload is not automatically converted to a string. // This is to reduce the number of customizations that create inconsistent behaviors. const payloadIsByteArray = invoke.Payload instanceof Uint8Array; console.log("Invoke response payload type is Uint8Array:", payloadIsByteArray); // To maintain the old functionality, only one additional method call is needed: // v3 adds a method to the Uint8Array called transformToString. const payloadObject = JSON.parse(invoke.Payload.transformToString()); console.log("Invoke response object", payloadObject);
Amazon SQS
MD5 校验和
要跳过对消息正文 MD5 校验和的计算,请在配置对象上设置md5
为 false。否则,默认情况下,SDK 将计算发送消息的校验和,以及验证检索到的消息的校验和。
// Example: Skip MD5 checksum in Amazon SQS import { SQS } from "@aws-sdk/client-sqs"; new SQS({ md5: false // note: only available in v3.547.0 and higher });
QueueUrl
在 Amazon SQS 操作中使用以此为输入参数的自定义时,在 v2 中,可以提供一个将替换 Amazon SQS 客户端默认终端节点的自定义QueueUrl
端点。
多区域消息
在 v3 中,你应该为每个区域使用一个客户端。该 Amazon 区域应在客户端级别进行初始化,并且不会在请求之间进行更改。
import { SQS } from "@aws-sdk/client-sqs"; const sqsClients = { "us-east-1": new SQS({ region: "us-east-1" }), "us-west-2": new SQS({ region: "us-west-2" }), }; const queues = [ { region: "us-east-1", url: "https://sqs.us-east-1.amazonaws.com/{AWS_ACCOUNT}/MyQueue" }, { region: "us-west-2", url: "https://sqs.us-west-2.amazonaws.com/{AWS_ACCOUNT}/MyOtherQueue" }, ]; for (const { region, url } of queues) { const params = { MessageBody: "Hello", QueueUrl: url, }; await sqsClients[region].sendMessage(params); }
自定义终端节点
在 v3 中,当使用自定义终端节点(即与默认的 Amazon SQS 公共终端节点不同的终端节点)时,您应始终在 Amazon SQS 客户端上设置终端节点以及字段。
QueueUrl
import { SQS } from "@aws-sdk/client-sqs"; const sqs = new SQS({ // client endpoint should be specified in v3 when not the default public SQS endpoint for your region. // This is required for versions <= v3.506.0 // This is optional but recommended for versions >= v3.507.0 (a warning will be emitted) endpoint: "https://my-custom-endpoint:8000/", }); await sqs.sendMessage({ QueueUrl: "https://my-custom-endpoint:8000/1234567/MyQueue", Message: "hello", });
如果您未使用自定义终端节点,则无需在客户端endpoint
上进行设置。
import { SQS } from "@aws-sdk/client-sqs"; const sqs = new SQS({ region: "us-west-2", }); await sqs.sendMessage({ QueueUrl: "https://sqs.us-west-2.amazonaws.com/1234567/MyQueue", Message: "hello", });