无法从 EC2 实例元数据服务检索凭据

Failed to retrieve credentials from EC2 Instance Metadata Service

我正在尝试使用 SDK 通过 AWS SES API 发送电子邮件。

我的代码基于此处的官方文档:https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-using-sdk.html

我已经到达 await client.SendEmailAsync(sendRequest); 并收到错误消息:

Failed to retrieve credentials from EC2 Instance Metadata Service.

// initialization
var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USWest2);
var response = new SendEmailResponse();

// build email
var sendRequest = new SendEmailRequest
{
    Source = ToAddress,
    Destination = new Destination
    {
        ToAddresses =
        new List<string> { ReceiverAddress }
    },
    Message = new Message
    {
        Subject = new Content(model.Subject),
        Body = new Body
        {
            Html = new Content
            {
                Charset = "UTF-8",
                Data = model.HtmlBody
            },
        }
    },
};

// send async call to api
try
{
    var response = await client.SendEmailAsync(sendRequest);
}
catch (Exception ex)
{

}

我已确认我的域已通过 AWS 控制台验证,并且还显示为 "Enabled for Sending"。

我哪里错了?

我找到了问题的答案。

可以通过创建 IAM 用户组和有权访问 SES 服务的用户来解决此问题。

然后我编辑了代码以传递 AccessKeyId 和 SecretAccessKey。

    var client = new AmazonSimpleEmailServiceClient(awsAccessKeyId, awsSecretAccessKey, RegionEndpoint.USWest2);
    var response = new SendEmailResponse();

这是有效的。但是,为了更安全,建议使用 Shared Credentials File

希望这对其他人有帮助。

编辑: 在 AWS SES SDK 的 V2 中,您需要将 AmazonSimpleEmailServiceClient 更改为 AmazonSimpleEmailServiceV2Client.

    var client = new AmazonSimpleEmailServiceV2Client(awsAccessKeyId, awsSecretAccessKey, RegionEndpoint.USWest2);
    var response = new SendEmailResponse();

以防有人来到这里时对从头开始配置 AWS 一无所知。只需添加一些细节:

之后我可以在本地访问 AWS 服务