dongyongmin5711 2018-06-13 04:34 采纳率: 100%
浏览 215

从AWS SES发送电子邮件时出现凭证错误

I am trying to send emails through Amazon SES in Golang. Previously it was working on a development server (without SSL) with following code:

sess, err := session.NewSession(&aws.Config{
            Region:aws.String(AwsRegion)},
        )

    /* Create an SES client in the session */
    svc := ses.New(sess)

    /* Assemble the email */
    input := &ses.SendEmailInput{
        Destination: &ses.Destination{
            CcAddresses: []*string{
            },
            ToAddresses: []*string{
                aws.String(request.EmailTo),
            },
        },
        Message: &ses.Message{
            Body: &ses.Body{
                Html: &ses.Content{
                    Charset: aws.String(CharSet),
                    Data:    aws.String(body),
                },
            },
            Subject: &ses.Content{
                Charset: aws.String(CharSet),
                Data:    aws.String(subject),
            },
        },
        Source: aws.String("test<test@example.com>"),
    }

    /* Attempt to send the email */
    result, err := svc.SendEmail(input)

Recently I started working on the production server (with SSL) and the same code started giving me following error:

NoCredentialProviders: no valid providers in chain. Deprecated.
        For verbose messaging see aws.Config.CredentialsChainVerboseErrors

I have tried adding credentials to the code like this:

creds := credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{})

/* updated session block with credentials in aws config */
sess, err := session.NewSession(&aws.Config{
    Region: aws.String(AwsRegion),
    Credentials: creds})

But this code is not working (not even compiling) and gives me following error:

/main.go:63:42: undefined: ec2rolecreds

However I have already included credentials package in code.

I am not getting how to add credentials to this code so that it works on production server too. Any kind of help will be appreciated.

  • 写回答

1条回答 默认 最新

  • dsfjnxjlbqv9812 2018-06-24 16:42
    关注

    The first error (NoCredentialProviders) isn't an issue with code but that it can't find any credentials. According to this page it automatically checks in the following order:

    1. Environment variables.
    2. Shared credentials file.
    3. If your application is running on an Amazon EC2 instance, IAM role for Amazon EC2.

    If it worked in one environment and not another I'm guessing you had an IAM role with the necessary permissions associated with an EC2 instance via an Instance Profile.

    To set credentials explicitly, try this:

    staticCreds = credentials.NewStaticCredentials("<key-id>", "<secret>", "<session token>")
    session, err = session.NewSession(&aws.Config{Credentials: staticCreds})
    
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数