AWS-SDK:S3的列表对象中的查询参数

时间:2019-02-06 17:20:17

标签: node.js amazon-web-services amazon-s3 aws-lambda aws-sdk

我想在特定日期之后获取对象。使用AWS CLI,我可以使用以下命令列出对象:

{"status":"Pulling from jenkins/jenkins","id":"latest"} {"status":"Pulling fs layer","progressDetail":{},"id":"ab1fc7e4bf91"} {"status":"Pulling fs layer","progressDetail":{},"id":"35fba333ff52"} {"status":"Pulling fs layer","progressDetail":{},"id":"f0cb1fa13079"} {"status":"Pulling fs layer","progressDetail":{},"id":"3d1dd648b5ad"} {"status":"Pulling fs layer","progressDetail":{},"id":"a9f886e483d6"} {"status":"Pulling fs layer","progressDetail":{},"id":"4346341d3c49"} .. "status":"Waiting","progressDetail":{},"id":"3d1dd648b5ad"} {"status":"Waiting","progressDetail":{},"id":"a9f886e483d6"} {"status":"Waiting","progressDetail":{},"id":"4346341d3c49"} {"status":"Waiting","progressDetail":{},"id":"006f2208d67a"} {"status":"Waiting","progressDetail":{},"id":"fb85cf26717d"} {"status":"Waiting","progressDetail":{},"id":"52ca068dbca7"} {"status":"Waiting","progressDetail":{},"id":"82f4759b8d12"} ... {"status":"Downloading","progressDetail":{"current":110118,"total":10780995},"progress":"[\u003e ] 110.1kB/10.78MB","id":"35fba333ff52"} {"status":"Downloading","progressDetail":{"current":457415,"total":45344749},"progress":"[\u003e ] 457.4kB/45.34MB","id":"ab1fc7e4bf91"} {"status":"Downloading","progressDetail":{"current":44427,"total":4340040},"progress":"[\u003e ] 44.43kB/4.34MB","id":"f0cb1fa13079"} {"status":"Downloading","progressDetail":{"current":817890,"total":10780995},"progress":"[===\u003e ] 817.9kB/10.78MB","id":"35fba333ff52"} {"status":"Downloading","progressDetail":{"current":1833671,"total":45344749},"progress":"[==\u003e ] 1.834MB/45.34MB","id":"ab1fc7e4bf91"} {"status":"Downloading","progressDetail":{"current":531179,"total":4340040},"progress":"[======\u003e ] 531.2kB/4.34MB","id":"f0cb1fa13079"} {"status":"Downloading","progressDetail":{"current":1719010,"total":10780995},"progress":"[=======\u003e ] 1.719MB/10.78MB","id":"35fba333ff52"} {"status":"Downloading","progressDetail":{"current":3205831,"total":45344749},"progress":"[===\u003e ] 3.206MB/45.34MB","id":"ab1fc7e4bf91"} {"status":"Downloading","progressDetail":{"current":1129195,"total":4340040},"progress":"[=============\u003e ] 1.129MB/4.34MB","id":"f0cb1fa13079"} {"status":"Downloading","progressDetail":{"current":2640610,"total":10780995},"progress":"[============\u003e ] 2.641MB/10.78MB","id":"35fba333ff52"} {"status":"Downloading","progressDetail":{"current":1719019,"total":4340040},"progress":"[===================\u003e ] 1.719MB/4.34MB","id":"f0cb1fa13079"} {"status":"Downloading","progressDetail":{"current":4586183,"total":45344749},"progress":"[=====\u003e ] 4.586MB/45.34MB","id":"ab1fc7e4bf91"} {"status":"Downloading","progressDetail":{"current":3549922,"total":10780995},"progress":"[================\u003e ] 3.55MB/10.78MB","id":"35fba333ff52"} {"status":"Downloading","progressDetail":{"current":2513643,"total":4340040},"progress":"[============================\u003e ] 2.514M ... {"status":"Pull complete","progressDetail":{},"id":"6d9b49fc8a28"} {"status":"Extracting","progressDetail":{"current":380,"total":380},"progress":"[==================================================\u003e] 380B/380B","id":"6302e8b6563c"} {"status":"Extracting","progressDetail":{"current":380,"total":380},"progress":"[==================================================\u003e] 380B/380B","id":"6302e8b6563c"} {"status":"Pull complete","progressDetail":{},"id":"6302e8b6563c"} {"status":"Extracting","progressDetail":{"current":1548,"total":1548},"progress":"[==================================================\u003e] 1.548kB/1.548kB","id":"7348f018cf93"} {"status":"Extracting","progressDetail":{"current":1548,"total":1548},"progress":"[==================================================\u003e] 1.548kB/1.548kB","id":"7348f018cf93"} {"status":"Pull complete","progressDetail":{},"id":"7348f018cf93"} {"status":"Extracting","progressDetail":{"current":3083,"total":3083},"progress":"[==================================================\u003e] 3.083kB/3.083kB","id":"c651ee7bd59e"} {"status":"Extracting","progressDetail":{"current":3083,"total":3083},"progress":"[==================================================\u003e] 3.083kB/3.083kB","id":"c651ee7bd59e"} {"status":"Pull complete","progressDetail":{},"id":"c651ee7bd59e"} {"status":"Digest: sha256:abd3e3f96fbc3445c420fda590f37e2bd3377f69affd47b63b3d826d084c5ddc"} {"status":"Status: Downloaded newer image for jenkins/jenkins:latest"}

但是我想从我的代码中做到这一点,所以请让我知道如何在NPM AWS-SDK中使用对象过滤对象。

注意:我可以使用aws s3api list-objects-v2 --bucket "bucket1" --prefix "file-" --query "(Contents[?LastModified>'2019-02-06T05:34:12.000Z'])[0]"exec来做到这一点,但是为此,我必须使用CLI配置配置文件,因为它将在本地创建凭证文件,所以我不想这样做。

1 个答案:

答案 0 :(得分:1)

使用AWS SDK for node.js。调用listObjectsV2方法,然后在回调方法中使用jmespath.js来过滤API调用的输出。这等效于AWS CLI通过--query参数进行的操作。

这样的东西(未经测试)

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);
   }
 };