AWS 强迫我升级 SDK V3,现在我很难设置我的凭证。它们曾经是硬编码的,例如:
AWS.config.update({
apiVersion: "2010-12-01",
accessKeyId: "MYKEY",
secretAccessKey: "MYOTHERKEY",
region: "us-east-1",
});
但现在
AWS
软件包已被弃用,取而代之的是模块化 @aws-sdk/client-ses
。
如何像以前在新版本 SDK 中那样对我的凭据进行硬编码?
到目前为止我所拥有的:
import {
SESClient,
CloneReceiptRuleSetCommand,
} from "@aws-sdk/client-ses";
const client = new SESClient({
accessKeyId: "MYKEY",
secretAccessKey: "MYOTHERKEY",
region: "us-east-1",
});
const command = new CloneReceiptRuleSetCommand(params);
client.send(command)
但它返回错误“CredentialsProviderError:无法从任何提供商加载凭据”
P.S.:我知道硬编码凭证的缺点,但这对于这个应用程序来说并不是一个特别的问题。这是一个后端 Node.js 服务,只有我需要访问它。
密钥和秘密需要位于配置对象的
credentials
对象中。
此外,对于
CloneReceiptRuleSetCommand
,您需要提供OriginalRuleSetName
和RuleSetName
。
所以,应该是这样的:
import {
SESClient,
CloneReceiptRuleSetCommand,
} from "@aws-sdk/client-ses";
const client = new SESClient({
credentials: {
accessKeyId: "MYKEY",
secretAccessKey: "MYOTHERKEY"
},
region: "us-east-1",
});
const params = {
OriginalRuleSetName: 'RULESET_TO_CLONE',
RuleSetName: 'TARGET_RULESET'
}
const command = new CloneReceiptRuleSetCommand(params);
client.send(command)
参考资料:
我想为本地和部署的凭据设置提供我的解决方案:
我需要向 SESClient 提供凭证,并且我已经使用代码设置了一个 AWS amplify(带有 aws-sdk v3 的代码优先 gen2)项目。很多配置都是自动处理的,很难找到使用过的角色和发行版,但这对我有用。
const sesClient = new SESClient({
region: REGION,
credentials: fromIni({ profile: "[PROFILE_NAME]" }),
设置本地 aws 配置文件后,您可以在此处指定它。您也可以简单地使用
fromIni
,然后采用默认配置文件。
import { CognitoIdentityClient } from "@aws-sdk/client-cognito-identity";
import { fromCognitoIdentityPool } from "@aws-sdk/credential-provider-cognito-identity";
const REGION = "eu-central-1";
const IDENTITY_POOL_ID = "eu-central-1:xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx";
const cognitoIdentityClient = new CognitoIdentityClient({ region: REGION });
const credentials = fromCognitoIdentityPool({
client: cognitoIdentityClient,
identityPoolId: IDENTITY_POOL_ID,
});
const sesClient = new SESClient({
region: REGION,
credentials: credentials
});
Amplify 会自动创建一个 Cognito 身份池 ID,可以在
amplifyconfiguration.json
文件中找到该 ID。添加此后,还需要向 Cognito 规则添加必要的策略。
在查看 Cloudwatch 日志中的错误后,我找到了正确的角色:
Error sending email in handler: AccessDenied: User `arn:aws:sts::1234567890:assumed-role/amplify-[NAME]-amplifyAuthunauthenticate-[ID]/CognitoIdentityCredentials' is not authorized to perform `ses:SendTemplatedEmail' on resource `[RESOURCE_ARN]'
希望这对尝试设置客户端并需要在本地或云端提供凭据的任何人有用。