简体   繁体   中英

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.

There is a csv file that is located in an Amazon S3 bucket. I have to send an email to the email placed in the second column if the value in the first column equals to high .

Metric Email address
High address1@email.com
Low address2@email.com

Is this achievable just by using Lambda and SES, or do I have to use anything else?

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):

  1. Create a Lambda function that uses the S3 and SES APIs. The IAM role you need for the Lambda function needs S3 and SES permissions.

  2. Call the S3 getObjectAsBytes method to get the CSV file.

  3. Read the CSV and for each email address where there is a High value, invoke the SES send email method.

Here is the S3 Code:

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

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

Here is the SES Code to send an email.

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.

From the event data, you will know the name of the object that was uploaded. In the lambda, get the contents of said file from S3.

If the criteria is met, then use SES from within the lambda to send an 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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