Is there a way to trigger lambda only after multip

2020-05-10 07:21发布

问题:

A user uploads multiple files into my S3 bucket with the current day as prefix for all the files. I need to trigger a lambda function only after I have received all the files under the prefix. How can I do that?.

回答1:

Create a DynamoDb table to keep track of the uploaded parts. You should use a HASH key to store the prefix of the files, or something like that. Another attribute could be a count of parts.

On each part uploaded, a lambda will be called and it will update the record at the table in this way:

  1. Read the record.

  2. If the record does not exist, create it, with count = 1, using a conditional expression to create only if the record still not exists. This is to avoid one lambda overriding another.

    • If error, back to 1.
  3. If the record exists, update it incrementing the count with the conditional expression to update only if the count is equal the count read on 1.

    • If error, back to 1.
  4. When you reach the expected count, invoke the lambda to process it.