发送电子邮件时如何为Amazon SES服务提供AWS密钥?

时间:2018-05-17 23:41:11

标签: java amazon-web-services amazon-ses

文档说您需要一个共享凭据文件'在您的主目录中,但这不适用于生产!那么如何在发送电子邮件时指定AWS密钥?它没有给出任何例子!

https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-using-sdk.html

  

此代码示例假定您已安装AWS SDK for Java,并且您已创建共享凭据文件。

  AmazonSimpleEmailService client = 
      AmazonSimpleEmailServiceClientBuilder.standard()
      ...
  client.sendEmail(request);

他们的参考文档甚至不包含AmazonSimpleEmailServiceClientBuilder

https://docs.aws.amazon.com/search/doc-search.html?searchPath=documentation-guide&searchQuery=AmazonSimpleEmailServiceClientBuilder&x=3&y=8&this_doc_product=Amazon+Simple+Email+Service&this_doc_guide=API+Reference&doc_locale=en_us#facet_doc_product=Amazon%20Simple%20Email%20Service&facet_doc_guide=API%20Reference

1 个答案:

答案 0 :(得分:3)

如果您使用的是EC2或Lambda或任何AWS IAM role支持的服务,则安全的方式是创建一个可以访问SES并将其附加到资源的角色。您无需维护任何凭据。 https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#create-iam-role

以下链接提供了为您的Java程序提供凭据的不同方法 https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html

如果你想在你的程序中提供

AWSCredentialsProvider credentialsProvider = new AWSCredentialsProvider() {
    @Override
    public void refresh() {}
        @Override
        public AWSCredentials getCredentials() {
        return new AWSCredentials() {
            @Override
            public String getAWSSecretKey() {
                return "YOUR_SECRET_KEY";
            }
            @Override
            public String getAWSAccessKeyId() {
                return "YOUR_ACCESS_KEY";
            }
        };
    }
};

AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard()
            .withCredentials(credentialsProvider).build();