无法从 EC2 实例元数据服务检索凭证 [英] Failed to retrieve credentials from EC2 Instance Metadata Service

查看:27
本文介绍了无法从 EC2 实例元数据服务检索凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用开发工具包通过 AWS SES API 发送电子邮件.

I'm trying to send an email via the AWS SES API using the SDK.

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

I based my code off the official documentation here: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-using-sdk.html

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

I'm getting as far as await client.SendEmailAsync(sendRequest); and receive the error message:

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

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 控制台进行验证,并且它也显示为已启用发送".

I've confirmed that my domain is verified via the AWS console and it's also showing as "Enabled for Sending".

我哪里出错了?

推荐答案

我找到了问题的答案.

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

The issue can be resolved by creating an IAM user group and user with access to the SES service.

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

Then I edited my code to pass the AccessKeyId and SecretAccessKey.

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

这是有效的.但是,为了更安全,建议使用 Shared使用凭据文件.

This is working. However to be more secure it's recommended that a Shared Credentials File be used.

希望这对其他人有帮助.

Hope this helps someone else.

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

In V2 of the AWS SES SDK you need to change AmazonSimpleEmailServiceClient to AmazonSimpleEmailServiceV2Client.

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

这篇关于无法从 EC2 实例元数据服务检索凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆