简体   繁体   English

AWS分页listObjects node.js

[英]AWS Pagination listObjects node.js

Hi I am currently wanting to list images within a store with the use of pagination. 您好,我目前想使用分页功能列出商店中的图片。 I need to display a specified number of images each time and allow the loop to stop on the page number i have specified. 我每次需要显示指定数量的图像,并允许循环在我指定的页码上停止。 (example. 30 images per page and I need page 3 I will need to list the 3rd page of images). (例如,每页30张图像,我需要第3页,我需要列出图像的第3页)。 So far it iterates through all the files, I just need to list objectsPerPage depending on the pageCount 到目前为止,迭代所有文件,我只需要根据pageCount列出objectsPerPage

(GET /list/{storeid}?page={pageNumber}&per_page={perPage} (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);
        }
    }
});
}

A solution that I eventually did was create a pageCounter variable and converted the continuation tokens into integer values. 我最终要做的一个解决方案是创建一个pageCounter变量,并将延续标记转换为整数值。 This then incremented as the files are looped through providing a page count. 然后,在通过提供页数循环文件时,此计数会增加。 A simple if statement is also constructed so that when the pageCount is equal to the page requested it will console.log out the data. 还构造了一个简单的if语句,以便当pageCount等于请求的页面时,它将控制台。注销数据。

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

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

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