简体   繁体   English

S3的Node.js / Awssum

[英]Node.js / Awssum for S3

Hi there: I'm wondering if anyone knows a method for listing all files inside of an S3 bucket with node.js and Awssum, starting with the most recent file first. 您好:我想知道是否有人知道一种使用node.js和Awssum列出S3存储桶中所有文件的方法,首先从最新文件开始。

By default, my code takes the first created file. 默认情况下,我的代码采用第一个创建的文件。

function getFileList(callback){
s3.ListObjects(coldBucket, function(err, data) {
    var fileList = getFilenames(data, coldBucketPath);
    callback(fileList);
});
};

Any help is much appreciated! 任何帮助深表感谢! Thanks. 谢谢。

this is Andy, creator of AwsSum. 这是AwsSum的创建者Andy。

That's correct, there is no way of asking S3 for the filenames in order of inserted time. 没错,没有办法按照插入时间的顺序向S3询问文件名。 However, you can get it using the LastModified. 但是,您可以使用LastModified获得它。

If you'd like to see an example to get all the keys, see this example here: 如果您想查看获取所有密钥的示例,请参见此处的示例:

You could then store each item in an array, then sort it using LastModified. 然后,您可以将每个项目存储在数组中,然后使用LastModified对其进行排序。 The underscore library contains a function that would be useful for this: 下划线库包含一个对此有用的函数:

I looking at http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketGET.html , I don't believe s3 allows you to specify the sort order of the response. 我在看http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketGET.html ,我不相信s3允许您指定响应的排序顺序。

However, you may be able to work around that by changing your file structure to include folders named after the date they were created, then you could do a couple of ListObjects requests with prefixes for recent days/months/etc. 但是,您可以通过更改文件结构以包含以其创建日期命名的文件夹来解决此问题,然后可以执行几个带有最近几天/几个月/等前缀的ListObjects请求。

For example, you could have year-month folder names so your file structure looks like this: 例如,您可能具有year-month文件夹名称,因此您的文件结构如下所示:

bucket_name/
    - 2012-3/
        - file2.jpg
        - file3.jpg
    - 2012-2/
        - file1.jpg

and then before you make your request, do this 然后在您提出请求之前,执行此操作

var now = new Date();
coldBucket.Prefix = now.getFullYear() + "-" + now.getMonth();

And then if you wanted older stuff you'd have to do a followup request for previous months. 然后,如果您想要旧的东西,则必须对前几个月进行跟进请求。

(If you have too many files even for that, you could try year-month-day ) (如果您甚至有太多文件,则可以尝试year-month-day

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

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