Send Email With Multiple Attachments using SES - amazon-web-services

I have a task to send email with multiple attachments. S3 bucket will receive 2 files in same time approximately.
By using S3 bucket Put event, I am able to send email with single attachment by using lambda + SES.
Now the task is like,
I am getting 2 files in S3 like "XXXYYYZZZ" and "XXXYYYZZZ.20190712111820".
Prefix is same for both files and second file has name with its timestamp (20190712111820)
Here i need to send single email with above 2 files as attachment.
How to achieve it? I can understand Put Event will work on every new file gets created in S3.

I was able to achieve solution for the same by attaching multiple MimeBodyPart into the message.
For each attachment, create MimeBodyPart attachment, read it and add it to the MimeMultipart.
It worked fine for me.

Related

Why is lambda getting randomly timed out while trying to read the head object for a key on S3 bucket?

I am working on a feature where a user can upload multiple files which need to be parsed and converted to PDF if required. For that, I'm using AWS and when the user selects N files for upload then the following happens:
The client browser is connected to an AWS WebSocket API which is responsible for sending back the parsed data to respective clients later.
A signed URL for S3 is get from the webserver using which all of the user's files are uploaded onto an S3 bucket.
As soon as each file is uploaded, a lambda function is triggered for it which fetches the object for that file in order to get the content and some metadata to associate the files with respective clients.
Once the files are parsed, the response data is sent back to the respective connected clients via the WebSocket and the browser JS catches the event data and renders it.
The issue I'm facing here is that the lambda function randomly times out at the line which fetches the object of the file (either just head_object or get_object). This is happening for roughly 50% of the files (Usually I test by just sending 15 files at once and 6-7 of them fail)
import boto3
s3 = boto3.client("s3")
def lambda_handler(event, context):
bucket = event["Records"][0]["s3"]["bucket"]["name"]
key = urllib.parse.unquote_plus(event["Records"][0]["s3"]["object"]["key"], encoding="utf-8")
response = s3.get_object(Bucket=bucket, Key=key) # This or head_object gets stuck for 50% of the files
What I have observed is that even if the head_object or get_object is fetched for a file which already exists on S3 instead of getting it for the file who's upload triggered the lambda. Then also it times out with the same rate.
But if the objects are fetched in bulk via some local script using boto3 then they are fetched under a second for 15 files.
I have also tried using my own AWS Access ID and Secret key in lambda to avoid any issue caused by the temporarily generated keys.
So it seems that the multiple lambda instances are having trouble in getting the S3 file objects in parallel, which shouldn't happen though as AWS is supposed to scale well.
What should be done to get around it?

Upload a file to S3 via website, process with Lambda, send file back to user

I'm fairly new to AWS.
What I am trying to do is have a static website hosted on S3. The user would be able to upload a zip file to S3 via the front end. Once received, a lambda function would process the zip file and save the output to a single HTML file in another bucket.
How would I then return the processed file back to the right user via the front end? Do I need to send cookies or something along with the original file and somehow send it through lambda too? Additionally, is there a process that would return the processed file back to the original uploader directly from the processing lambda function?
Thanks All,
Appreciate it :)

AWS S3: Notification for files in particular folder

in S3 buckets we have a folder where incoming files are being placed. And then some of our system picks it up and processes it.
I want to know how many files in this folder is older than some period and then send a notification to corresponding team.
I.e. In S3 bucket, if some file arrived today and it's still there even after 3 hours, I want to get notified.
I am thinking to use boto python library to iterate through all the objects inside S3 bucket at schduled interval to check files are folder. And then send notification. However, this pulling solution doesn't seem good.
I am thinking to have some event based solution. I know, S3 has events which I can subscribe using either queue or lambda. However, I don't want to do any action as soon as I have file available, I just want to to check which files are older than some time and send email notification.
can we achieve this using event based solution?
Per hour we are expecting around 1000 files. Once file is processed they are moved to different folder. However if something goes wrong it will be there. So in day, I am not expecting more than 10,000 files in one bucket. Consider I have multiple buckets.
Itarate through S3 files to do that kind of filter is not a good idea. It can get very slow when you have more than a thousad of files in there. I would suggest you to use a database to store that records.
You can have a dynamodb with 2 columns: file name and upload date. Or, if budget is a problem, you can even have a sqlite3 file on the bucket, and fetch it whenever you need to query or add data to it. I did this using lambda, and it works just fine. Just don't forget to upload the file again when new records are inserted.
You could create an Amazon CloudWatch Event rule that triggers an AWS Lambda function at a desired time interval (eg every 5 minutes or once an hour).
The AWS Lambda function could list the desired folder looking for files older than a desired time period. It would be something like this:
import boto3
from datetime import datetime, timedelta, timezone
s3_client = boto3.client('s3')
paginator = s3_client.get_paginator('list_objects_v2')
page_iterator = paginator.paginate(
Bucket = 'my-bucket',
Prefix = 'to-be-processed/'
)
for page in page_iterator:
for object in page['Contents']:
if object['LastModified'] < datetime.now(tz=timezone.utc) - timedelta(hours=3):
// Print name of object older than given age
print(object['Key'])
You could then have it notify somebody. The easiest way would be to send a message to an Amazon SNS topic, and then people can subscribe to that topic via SMS or email to receive a notification.
The above code is quite simple in that it will find the same file every time, not just the new files that have been added to the notification period.

AWS SES + S3 : Send emails with attachment from S3

I'm using AWS SES service to send emails to my customers, I wonder if there's any solution available to attach files directly into my email using SES and Lambda functions. I did a research and ended up in finding solutions which recommends to include a link to S3 files, not attaching the file as it is. I want to attach files as it is from SE, which is downloadable from the email itself. Not a link or reference to the attachment.
As folks mentioned in the comments above, there's no way to automatically send a file "directly" from S3 via SES. It sounds like you will need to write a Lambda function which performs the following steps:
Fetch file object from S3 into memory
Build multi-part MIME message with text body and file attachment
Send your raw message through SES
Step 1 is a simple matter of using S3.getObject with the appropriate Bucket/Key parameters.
I do not know which language you are using, but in Node.js step #2 can be accomplished using the npm package mailcomposer like so:
const mailOptions = {
from: 'no-reply#example.tld',
to: 'whoever#example.tld',
subject: 'The Subject Line',
text: 'Body of message. File is attached...\n\n',
attachments: [
{
filename: 'file.txt',
content: fileData,
},
],
};
const mail = mailcomposer(mailOptions);
mail.build(<callback>);
Step 3 is again a simple matter of using SES.sendRawEmail with the RawMessage.Data parameter set to the message you built in step 2.
Nodemailer comes to mind.
There is a good medium tutorial covering how to do it here.

AWS S3 Event - Client Identification

I'm looking to allow multiple clients can upload files to an S3 bucket (or buckets). The S3 create event would trigger a notification that would add a message to an SNS topic. This works, but I'm having issues deciding how to identify which client uploaded the file. I could get this to work by explicitly checking the uploaded file's subfolder/S3 name, but I'd much rather automatically add the client identifier as an attribute to the SNS message.
Is this possible? My other thought is using a Lambda function as a middle man to add the attribute and pass it along to the SNS Topic, but again I'd like to do it without the Lambda function if possible.
The Event Message Structure sent from S3 to SNS includes a field:
"userIdentity":{
"principalId":"Amazon-customer-ID-of-the-user-who-caused-the-event"
},
However, this also depends upon the credentials that were used when the object was uploaded:
If users have their individual AWS credentials, then the Access Key will be provided
If you are using a pre-signed URL to permit the upload, then the Access Key will belong to the one used in the pre-signed URL and your application (which generated the pre-signed URL) would be responsible for tracking the user who requested the upload
If you are generating temporary credentials for each client (eg by calling AssumeRole, then then Role's ID will be returned
(I didn't test all the above cases, so please do test them to confirm the definition of Amazon-customer-ID-of-the-user-who-caused-the-event.)
If your goal is to put your own client identifier in the message, then the best method would be:
Configure the event notification to trigger a Lambda function
Your Lambda function uses the above identifier to determine which user identifier within your application triggered the notification (presumably consulting a database of application user information)
The Lambda function sends the message to SNS or to whichever system you wish to receive the message (SNS might not be required if you send directly)
You can add user-defined metadata to your files before you upload the file like below:
private final static String CLIENT_ID = "client-id";
ObjectMetadata meta = new ObjectMetadata();
meta.addUserMetadata(CLIENT_ID, "testid");
s3Client.putObject(<bucket>, <objectKey>, <inputstream of the file>, meta);
Then when downloading the S3 files:
ObjectMetadata meta = s3Client.getObjectMetadata(<bucket>, <objectKey>);
String clientId = meta.getUserMetaDataOf(CLIENT_ID);
Hope this is what you are looking for.