简体   繁体   English

AWS - 位于 Amazon S3 存储桶中的 CSV 文件中的 email 地址

[英]AWS - email address in a CSV file located in an Amazon S3 bucket

I'm a newbie in AWS environment and I can't really find a good answer to my problem.我是 AWS 环境的新手,我真的找不到解决我问题的好方法。

There is a csv file that is located in an Amazon S3 bucket. Amazon S3 存储桶中有一个 csv 文件。 I have to send an email to the email placed in the second column if the value in the first column equals to high .如果第一列中的值等于high ,我必须将 email 发送到第二列中的 email 。

Metric公制 Email address Email 地址
High高的 address1@email.com地址1@email.com
Low低的 address2@email.com地址2@email.com

Is this achievable just by using Lambda and SES, or do I have to use anything else?这是仅通过使用 Lambda 和 SES 就可以实现的,还是我必须使用其他任何东西?

Thank you for your help.谢谢您的帮助。

Yes, this is a valid use case.是的,这是一个有效的用例。 Here are High Level steps (I am answering this with assumption from Java API):以下是高级步骤(我在 Java API 的假设下回答这个问题):

  1. Create a Lambda function that uses the S3 and SES APIs.创建一个使用 S3 和 SES API 的 Lambda function。 The IAM role you need for the Lambda function needs S3 and SES permissions. Lambda function 所需的 IAM 角色需要 S3 和 SES 权限。

  2. Call the S3 getObjectAsBytes method to get the CSV file.调用S3 getObjectAsBytes方法获取CSV文件。

  3. Read the CSV and for each email address where there is a High value, invoke the SES send email method.读取 CSV 并为每个email地址调用 SES 发送 email 方法。

Here is the S3 Code:这是 S3 代码:

GetObjectRequest objectRequest = GetObjectRequest
                    .builder()
                    .key(keyName)
                    .bucket(bucketName)
                    .build();

 ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);

Here is the SES Code to send an email.这是发送 email 的 SES 代码。

public static void send(SesClient client,
                            String sender,
                            String recipient,
                            String subject,
                            String bodyText,
                            String bodyHTML
    ) throws MessagingException {

        Destination destination = Destination.builder()
                .toAddresses(recipient)
                .build();

        Content content = Content.builder()
                .data(bodyHTML)
                .build();

        Content sub = Content.builder()
                .data(subject)
                .build();

        Body body = Body.builder()
                .html(content)
                .build();

        Message msg = Message.builder()
                .subject(sub)
                .body(body)
                .build();

        SendEmailRequest emailRequest = SendEmailRequest.builder()
                .destination(destination)
                .message(msg)
                .source(sender)
                .build();

        try {
            System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
            client.sendEmail(emailRequest);

        } catch (SesException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }   

Following this tutorial , create a lambda function that will be triggered on an S3 object upload.按照本教程,创建一个 lambda function 将在 S3 object 上传时触发。

From the event data, you will know the name of the object that was uploaded.从事件数据中可以知道上传的object的名字。 In the lambda, get the contents of said file from S3.在 lambda 中,从 S3 获取上述文件的内容。

If the criteria is met, then use SES from within the lambda to send an email.如果满足条件,则使用 lambda 中的 SES 发送 email。

NB You'll need to configure the lambda function to have an IAM role to read from S3, as well as to send an email with SES.注意您需要将 lambda function 配置为具有从 S3 读取的 IAM 角色,以及使用 SES 发送 email。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM