Actually From my application they will generate lot of zip files and uploaded into s3 bucket.
The zip files name will be like dfghgghg5565hgghghgh55.zip, fdfdfdfd44545ghghghg.zip.It can be any name.
So How to download those zip files based on file extension(.zip)?
If you know the bucket that you're working on you can call ListObjects or ListObjectsv2 on that bucket to get up to 1000 objects from it.
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listObjects-property
var params = {
Bucket: 'STRING_VALUE', /* required */
Delimiter: 'STRING_VALUE',
EncodingType: url,
Marker: 'STRING_VALUE',
MaxKeys: 0,
Prefix: 'STRING_VALUE',
RequestPayer: requester
};
s3.listObjects(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
// put code here to filter out just the .zips, then you can request those files with a GetObject() request
});
You can call your lambda function manually whenever you want to get information about new files. With this code you will always get the last file modified / created.
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('.zip')) {
//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!