简体   繁体   English

如何仅从AWS S3对象获取顶层文件?

[英]How to get only the top layer files from AWS S3 object?

I use Javascript to get the file names from AWS S3 bucket. 我使用Javascript从AWS S3存储桶获取文件名。 After i get the complete list of file names from the bucket, i parse & manipulate it & list them in front-end. 从存储桶中获得文件名的完整列表后,我将对其进行解析和处理并将其列出在前端。 In this case, i want to get rid of the problem which will arise when the bucket with filled with huge amount of data (ie) i'll run out of memory when i try to manipulate with a shocking amount of data. 在这种情况下,我想摆脱当充满大量数据的存储桶(即)在尝试处理大量数据时会用完内存的问题。 So, i only need the file names of the very first layer. 因此,我只需要第一层的文件名。

Example : 范例:

The complete object in S3 bucket : { new_folder: {...}, some_file.png: {...} } S3存储桶中的完整对象: { new_folder: {...}, some_file.png: {...} }

Here i only need the names -> new_folder, some_file.png 在这里我只需要名称-> new_folder,some_file.png

Below is the code which i use now : 下面是我现在使用的代码:


const AWS = require('aws-sdk');
export default async function wasabiActions(dataObj) {
    var accessKeyId = '************';
    var secretAccessKey = '********************';

    var wasabiEndpoint = new AWS.Endpoint('s3.us-west-1.wasabisys.com');

    var s3 = await new AWS.S3({
        endpoint: wasabiEndpoint,
        accessKeyId: accessKeyId,
        secretAccessKey: secretAccessKey
    });

    var params = {
        Bucket: 'bucket_name',
    };

    s3.listObjectsV2(params, function(err, data) {
        if (!err) {
            var files = [];
            data.Contents.forEach(function(element) {
                files.push(element.Key.split('/').filter((name) => name.length > 0));
            });

            console.log(files);
            var parsedData = wasabiDataParser(files);
            console.log(parsedData);
        }
    });
}

Thanks in advance! 提前致谢! :) :)

You can use pagination. 您可以使用分页。 Some AWS operations return results that are incomplete and require subsequent requests in order to attain the entire result set. 某些AWS操作返回的结果不完整,需要后续请求才能获得整个结果集。 Check here for more details. 在此处查看更多详细信息。 https://boto3.amazonaws.com/v1/documentation/api/latest/guide/paginators.html https://boto3.amazonaws.com/v1/documentation/api/latest/guide/paginators.html

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

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