要使用AWS NodeJS SDK V3设置显式凭证,你可以按照以下步骤进行操作:
npm install aws-sdk
const { SQSClient, SendMessageCommand } = require("@aws-sdk/client-sqs");
const { fromCognitoIdentityPool } = require("@aws-sdk/credential-provider-cognito-identity");
// 用于设置AWS区域和凭证
const clientConfig = {
region: "your_region", // 替换为你的AWS区域
credentials: fromCognitoIdentityPool({
identityPoolId: "your_identity_pool_id" // 替换为你的身份池ID
})
};
// 创建SQS客户端
const sqsClient = new SQSClient(clientConfig);
以上代码使用了fromCognitoIdentityPool
凭证提供程序,并传递了你的Cognito身份池ID。确保将your_region
和your_identity_pool_id
替换为你的实际值。
// 发送消息的示例
const sendCommand = new SendMessageCommand({
QueueUrl: "your_queue_url", // 替换为你的队列URL
MessageBody: "Hello from AWS SDK"
});
(async () => {
try {
const response = await sqsClient.send(sendCommand);
console.log("Message sent:", response.MessageId);
} catch (error) {
console.error("Error sending message:", error);
}
})();
以上代码创建了一个SendMessageCommand
实例,并通过send
方法将消息发送到指定的队列。确保将your_queue_url
替换为你的实际队列URL。
通过这些步骤,你可以使用AWS NodeJS SDK V3设置显式凭证并发送消息到SQS队列。请注意,凭证的设置可能因你的具体情况而有所不同,比如使用不同的凭证提供程序或者直接提供凭证。