AWS s3 listobjects with pagination

时间:2015-06-10 11:35:53

标签: javascript node.js amazon-web-services amazon-s3 pagination

我想使用aws s3实现分页。对象ms.files中有500个文件,但我想一次只检索20个文件,下次再检索20个文件,依此类推。

var params = {
  Bucket: 'mystore.in',
  Delimiter: '/',
  Prefix: '/s/ms.files/',
  Marker:'images',
};
s3.listObjects(params, function(err, data) {
  if (err) console.log(err, err.stack); 
  else     console.log(data);          
});

2 个答案:

答案 0 :(得分:2)

Mr Jarmod分享的解决方案:

var params = {
  Bucket: 'mystore.in',
  Delimiter: '/',
  Prefix: '/s/ms.files/',
  Marker:'',
  MaxKeys : 20
};
s3.listObjects(params, function(err, data) {
  if (err) console.log(err, err.stack); 
  else     console.log(data);          
});

答案 1 :(得分:1)

在试图一次列出所有对象的同时,遇到这种情况,如果您的响应被截断,则会为您提供标志isTruncated = true和下一个调用的continuationToken

如果您在es6上可以这样做,

const AWS = require('aws-sdk');
const s3 = new AWS.S3({});

const listAllContents = async ({ Bucket, Prefix }) => {
  // repeatedly calling AWS list objects because it only returns 1000 objects
  let list = [];
  let shouldContinue = true;
  let nextContinuationToken = null;
  while (shouldContinue) {
    let res = await s3
      .listObjectsV2({
        Bucket,
        Prefix,
        ContinuationToken: nextContinuationToken || undefined,
      })
      .promise();
    list = [...list, ...res.Contents];

    if (!res.IsTruncated) {
      shouldContinue = false;
      nextContinuationToken = null;
    } else {
      nextContinuationToken = res.NextContinuationToken;
    }
  }
  return list;
};