Question:
I want to fetch objects after a particular date. Using the AWS CLI I can list objects using below command:
aws s3api list-objects-v2 --bucket "bucket1" --prefix "file-" --query "(Contents[?LastModified>'2019-02-06T05:34:12.000Z'])[0]"
But I want to do it from my code so please let me know how can I filter objects usin NPM AWS-SDK.
Note: I can do it using exec
or spawn
but for this I have to configure profile using CLI which will create credential file on local so I dont want to do this.
Answer:
Use the AWS SDK for node.js. Call the listObjectsV2
method and then use jmespath.js in the callback method to filter the output of the API call. This is the equivalent of what the AWS CLI does via the --query
param.
Something like this (untested)
1 2 3 4 5 6 7 8 9 10 11 12 |
var params = { Bucket: "bucket1", Prefix: "file-" }; s3.listObjectsV2(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else { query = "Contents[?LastModified>'2019-02-06T05:34:12.000Z']" var results = jmespath.search(data,query); } }; |