0
votes

I created a lambda function that is triggered using S3 PutObject event. My S3 bucket looks liks this:

s3://my-bucket-name/directory_1/...
s3://my-bucket-name/directory_2/...
s3://my-bucket-name/directory_3/...

Another application is creating folders like directory_1 and so on. Once data is created in this directory, the application also creates a file called _SUCCESS. so one of the directories look like this:

s3://my-bucket-name/directory_1/data.txt
s3://my-bucket-name/directory_1/_SUCCESS

Now I want to trigger my lambda function as soon as this _SUCCESS file is created. so I added a trigger of S3 in lambda as follows:

Event type: ObjectCreatedByPut
Bucket: my-bucket-name
Prefix: directory
Suffix: _SUCCESS

Currently, I'm able to see that lambda function is triggered properly whenever a new _SUCCESS file is created any directory. But I also want to know what is the exact key of the _SUCCESS file that triggered this function. How to do this?

For example, if s3://my-bucket-name/directory_1/_SUCCESS triggers my lambda, I should be able to get full path of this file inside this lambda function.

1
Your Lambda function receives two parameters: event and context. If you printed those out you would have your answer. Note that you should trigger on s3:ObjectCreated:*. - jarmod
Why ObjectCreated.*? - padmanabh pande
There are multiple APIs for uploading objects to S3. You trigger on s3:ObjectCreated:* to request notification when an object is created regardless of the API used. - jarmod

1 Answers

1
votes

S3 notification event structure is described in details here.

For example to get bucket name from the event (assuming 1 record in the event):

bucket_name = event['Records'][0]['s3']['bucket']['name']

Similarly for key:

key_name = event['Records'][0]['s3']['object']['key']