具有分页功能的AWS s3列表对象 [英] AWS s3 listobjects with pagination

查看:80
本文介绍了具有分页功能的AWS s3列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to implement pagination using 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.

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

推荐答案

与此同时,希望一次列出所有对象,如果您的响应被截断了,它会给您一个标志 isTruncated = true >和下一个呼叫的continuationToken

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

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

If youre on es6 you could do this,

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

这篇关于具有分页功能的AWS s3列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆