简体   繁体   English

带分页的 AWS s3 列表对象

[英]AWS s3 listobjects with pagination

I want to implement pagination using aws s3.我想使用 aws s3 实现分页。 There are 500 files in object ms.files but i want to retrieve only 20 files at a time and next 20 next time and so on.对象 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);          
});

Came across this while looking to list all of the objects at once, if your response is truncated it gives you a flag isTruncated = true and a continuationToken for the next call在寻找一次列出所有对象时遇到了这个问题,如果您的响应被截断,它会为您提供一个标志isTruncated = true和一个用于下一次调用的 continuationToken

If youre on es6 you could do this,如果你在 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;
};

Solution as shared by Mr Jarmod : 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);          
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM