Customizing / Updating the Default S3 bucket notification message - amazon-web-services

We are planning to use s3 bucket event notification to use for further processing. Our requirement is,
When an object is PUT / POST / COPY to s3 bucket, an event notification is generated.
The destination for this generated event notification is SQS.
We have tested 1st and 2nd part. But we are not getting an idea about how to customize the the default notification content to suit our processing.
We went thorough AWS dev guide. But, we could not find the expected solution.

The s3 event notification does not contain something like subject or message so I dont think you can change much of the generated JSON (see http://docs.aws.amazon.com/AmazonS3/latest/dev/notification-content-structure.html)
Each notification is delivered as a JSON object with the following fields:
Region Timestamp
Event Type (PUT/COPY ...)
Request Actor Principal
ID Source IP of the request Request
ID Host ID Notification
Configuration Destination ID
Bucket Name
Bucket ARN
Bucket Owner
Principal ID
Object Key
Object Size
Object ETag
Object Version ID (if versioning is enabled on the bucket)
You might have better chance to send a custom notification by running a lambda function (http://docs.aws.amazon.com/lambda/latest/dg/with-s3.html)

Related

How to send S3 object path or URI when an object created event is generated

I am new to AWS, I wanted to get S3 object URI/path when the object creation event is generated. Object can be created anywhere in the bucket, like there can be multiple sub folders that dynamically created in s3 bucket based on the date. So I want to know exactly where object is created. Is there anyway to do so? Seems like most of the message structure examples I can see only object name and bucket name not the entire object URI. I'm planning to use this message to SNS or kinesis streams with eventbridge.
You have to setup S3 Event Notifications for your bucket that will trigger your custom lambda function.
Once the object is uploaded to the bucket, your function is going to get invoked and obtain all the associated data about the event, such as object name, key, bucket, data, etc. Then your lambda may use that information to construct custom messages (entire object URI) that will be then uploaded to SNS or Kinesis.

How do we raise an event which stores the file name in SQS queue thru lambda, when a new file is uploaded in Amazon S3 bucket?

How do we raise an event which stores the file name in SQS queue thru lambda, when a new file is uploaded in Amazon S3 bucket
AWS S3 offers a feature specifically for this use case called "Amazon S3 Event Notifications".
You can configure your S3 bucket to send out a "notification" (also called event) to a SQS queue.
Furthermore, you can fine-tune for which specific "event" you want to get a notification. You could configure the bucket to send an event to SQS only when an object with the extension .jpg was deleted for example.
You should read the very detailed documentation by AWS to find out more:
Amazon S3 Event Notifications - Amazon Simple Storage Service

How do I add an alert for when something has been added to a specific folder in an S3 bucket?

I have an S3 bucket and would like an alert to go off when something has been added to the data_errors folder only.
I have already done some research and noticed I can add an event for an S3 bucket for POST command but it doesn't really tell me how to specify a specific folder.
Is there a way? Sorry, it's not clear from the docs.
Yes you can , There is a Prefix option available while adding an event, you need to set it to "data_errors/"
When configuring the Amazon S3 Event, you can specify a Prefix. This is effectively the same as a folder name:
You can configure the event to send a message to an Amazon SNS topic (great for sending email notifications) or an Amazon SQS queue, or to trigger an AWS Lambda function.

Reading incoming emails saved to s3 bucket in SES

I have configured AWS SES for sending and receiving emails I have verified my domain and created rule set by which all incoming emails will now be stored in an S3 bucket with object key prefix as email. I found the following codes for reading files from an S3 bucket:
http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingJava.html
I am trying to read emails. My rule for SES stores all incoming emails to my specified s3 bucket. I am trying to add the code that reads the bucket, get the emails. The next time when I read the bucket, how can I understand which emails were read before and which to read. So is there any way I could read the bucket with emails and them mak them as read so that I dont have to process them again
S3 is just storage. It has no sense of "read" vs "unread," and if you're discovering messages by listing objects in the bucket, your best solution would be something like this:
After processing each message, move it somewhere else. This could be another bucket, or a different prefix in the same bucket.
S3 doesn't have a "move" operation, but it does have copy and it does have delete... so, for each message you process, modify the object key (the path+filename).
If your emails are being stored with a prefix, like "incoming/" so that an individual message has a key that looks like (e.g.) "incoming/jozxyqkblahexample," change that string to "processed/jozxyqkblahexample." Then tell S3 to copy from the old to the new. When that succeeds, tell S3 to delete the original.
This (mostly? solves your problem, because since you only list objects with the prefix "incoming/" then you won't see those the next time -- they're now out of the way.
But, there's one potential problem with this solution... specifically, you may run afoul of the S3 consistency model. S3 does not guarantee that fetching a list of objects will immediately give you a response that reflects all of your recently-completed activity against the bucket... it's possible for objects to linger for a brief time in the object listing after being deleted... so it's still possible to see a message in the listing after you've deleted it. The chances are reasonably low, but you need to be aware of the possibility.
When SES drops a message into your bucket, it's also possible to configure it to notify you that it just did that.
Typically, a better solution than polling the bucket for mail is for SES to send you an SNS notification that the message was received. The notification will include information about the message, including the key where it was stored in the bucket. You then fetch exactly that message from the bucket, and process it, so no bucket object listing is needed.
Note that SES has two different notification types -- for small emails, SES can actually include the mail in the SNS notification, but that'a not the notification type referred to, above. Above, I'm suggesting that you investigate the possibility of using an alert notification, sent by SES through SNS to tell you about each email as it is dropped into S3.

Is there any way to add the specific object key and value tag automatically when user upload the file to AWS S3 bucket

I want to add the automatic key and value pair of TAG to be added with the object uploaded via AWS console ,
Example : when a IAM user uploads a file then by default the key has to be CREATEDBY and the value has to be his arn.
I want this condition to be achieved because I want to restrict other users to see / download object uploaded by other IAM users in the same folder using iam user policy by checking object tag values,
My requirement doesn't allow me to create multiple folder for different users as they are too many.
You can use a Lambda function that will be triggered when a new file is uploaded to your bucket. This function would in turn add the tag to the S3 object. Here's a tutorial to help you wire your S3 bucket to your Lambda function.
The event you will receive in your Lambda will be structured like this. From within your Lambda, you can retrieve the principalId field, which will give you information about the user who created the S3 object, as well as the S3 object's key. You can then use that information to tag the S3 object.