gpt4 book ai didi

javascript - AWS Pagination listObjects node.js

转载 作者:太空宇宙 更新时间:2023-11-03 22:26:18 26 4
gpt4 key购买 nike

您好,我目前想使用分页列出商店内的图像。我需要每次显示指定数量的图像,并允许循环停止在我指定的页码上。 (例如,每页 30 个图像,我需要第 3 页,我需要列出第 3 页的图像)。到目前为止,它遍历了所有文件,我只需要根据 pageCount 列出objectsPerPage

(GET/list/{storeid}?page={pageNumber}&per_page={perPage}

var shopId = event.shopkeeper + "/";
var objectsPerPage = event.perPage;
var pageCount = event.pageNumber;

var params = {
Bucket: AWS_Bucket,
Delimiter: '/',
Prefix: shopId,
MaxKeys: objectsPerPage
}; var dataContents = [];

function s3ListObjects() {
s3.listObjects(params, function(err, data) {
if (err) {
console.log("listS3Objects Error:", err);
} else {
var contents = data.Contents;
dataContents = dataContents.concat(contents);
if (data.IsTruncated) {
params.Marker = contents[contents.length-1].Key;
s3ListObjects(params, callback);
} else {
console.log(dataContents);
}
}
});
}

最佳答案

我最终采取的解决方案是创建一个 pageCounter 变量并将延续标记转换为整数值。然后,随着文件循环提供页数,该值会增加。还构建了一个简单的 if 语句,以便当 pageCount 等于请求的页面时,它将 console.log 出数据。

var s3DataContents = [];
var pageCounter = 0;

function s3ListObjects() {
s3.listObjectsV2(params, function(err, data) {
if (err) {
console.log("listS3Objects Error:", err);
} else {
if(data.IsTruncated) {
if(pageCounter == page){
s3DataContents = data.Contents;
s3DataContents.forEach(function(content){
let dates = new Date(content.LastModified).toLocaleString();
let str = content.Key;
str = str.split(shopId).pop();
return console.log("name: " + str + ";" + " created: " + dates + ";" + " size: " + content.Size + ";");
});
} else {
//if more files then iterate through
params.ContinuationToken = data.NextContinuationToken;
s3ListObjects(params, callback);
//convert string into integer value
params.ContinuationToken = parseInt(pageCounter);
pageCounter++;
}
} else {
callback("Images not found " + err);
}
}
});

关于javascript - AWS Pagination listObjects node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45842039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com