如何在AWS SES JS SDK V3中硬编码凭证?解决加载失败报错
AWS SDK V3 硬编码凭证配置方法(针对SES客户端)
你之前的代码报错是因为AWS SDK V3的客户端配置逻辑和V2不同——不能直接把accessKeyId和secretAccessKey放在客户端的顶层配置里,必须嵌套在credentials对象中,或者使用官方提供的凭证提供者函数。
下面是两种可行的硬编码配置方式:
方式一:直接嵌套credentials对象
这是最直接的写法,和V2的逻辑最接近:
import { SESClient, CloneReceiptRuleSetCommand } from "@aws-sdk/client-ses"; const client = new SESClient({ region: "us-east-1", credentials: { accessKeyId: "MYKEY", secretAccessKey: "MYOTHERKEY" } }); // 后续执行命令的代码保持不变 const params = { /* 填入你的CloneReceiptRuleSet参数 */ }; const command = new CloneReceiptRuleSetCommand(params); client.send(command);
方式二:使用fromStatic凭证提供者
这是SDK V3推荐的凭证配置方式,更符合模块化设计:
首先需要额外导入fromStatic函数,然后用它来生成凭证提供者:
import { SESClient, CloneReceiptRuleSetCommand } from "@aws-sdk/client-ses"; import { fromStatic } from "@aws-sdk/credential-providers"; const client = new SESClient({ region: "us-east-1", credentials: fromStatic({ accessKeyId: "MYKEY", secretAccessKey: "MYOTHERKEY" }) }); // 命令发送代码同上 const params = { /* 你的参数 */ }; const command = new CloneReceiptRuleSetCommand(params); client.send(command);
两种方式都能解决你遇到的CredentialsProviderError问题,根据自己的习惯选择即可。
内容的提问来源于stack exchange,提问作者Pedro Rabbi
相关产品推荐
相关产品推荐

