How to get recently uploaded zip file from s3 bucket when we trigger the lambda using node js

拈花ヽ惹草 提交于 2019-12-12 06:39:05

问题


I am uploading a zip file to s3 bucket,Once I uploaded the zip file,my lambda Function will get triggered.

Inside the Lambda Function block,I need to get the recently uploaded zip file name either based on Last Modified date of zip file from S3 bucket or Object Creation date from Lambda record event

However it may be ,But I need to get recently uploaded zip file name from s3 bucket.**

This is my code

s3.listObjects(params, function (err, data) {
    if (err)
        console.log(err, err.stack); // an error occurred


    var lastZipfile = null;
    var lastModified = null;
    data.Contents.forEach(function (c) {
        if (c.Key.endsWith('tar.gz')) {
            if (lastModified === null) {
                lastZipfile = c.Key;
                lastModified = c.LastModified;
            } else {
                // Compare the last modified dates
                if (lastModified <= c.LastModified) {
                    // Track the new latest file
                    lastZipfile = c.Key;
                    lastModified = c.LastModified;
                    //extractData(lastZipfile);
                }
            }
        }

    });
});

回答1:


I'll show you two options to solve this.


1º option (automatically):

The best option I see is to have a lambda function ready to run automatically every time a file is placed in bucket S3. When the lambda function is called, an event with information from the created file will be sent to the lambda function.

Here is an example of how to trigger:

next:

Here's an example to do this:

exports.handler = (event, context, callback) => {

  var lastCreatedFile = event.Records[0].s3.object.key;
  //extractData(lastCreatedFile);

};


2º option (manually):

However you can call your lambda function manually whenever you want to get information about new files. With your code you will always get the last file modified / created.

I've adjusted your lambda function that you've submitted to do this:

s3.listObjects(params, function (err, data) {
if (err)
    console.log(err, err.stack); // an error occurred

var sortArray;

data.Contents.sort(function(a,b) {
    return (b.LastModified > a.LastModified) ? 1 :
    ((a.LastModified > b.LastModified) ? -1 : 0);
});

for(var file of data.Contents){
    if (file.Key.endsWith('tar.gz')) {
        //extractData(file.Key);
        break;
    }
}

But we can have a problem like this, if there is no new file created, it will happen to extract the same file more than once. I suggest later that using the file delete or find another way to identify that file has already been used.


I hope it helped you!



来源:https://stackoverflow.com/questions/43467558/how-to-get-recently-uploaded-zip-file-from-s3-bucket-when-we-trigger-the-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!