简体   繁体   English

使用 AWS CLI 获取最新的 AWS S3 文件夹 Object 名称

[英]Grab latest AWS S3 Folder Object name with AWS CLI

I tried using this post to look for the last modified file then awk for the folder it's contained in: Get last modified object from S3 using AWS CLI我尝试使用这篇文章查找上次修改的文件,然后查找 awk 以查找它包含的文件夹: Get last modified object from S3 using AWS CLI

But this isn't ideal for over 1000 folders and by documentation , should be failing.但这对于超过 1000 个文件夹并不理想,并且根据文档,应该会失败。 I have 2000+ folder objects I need to search through.我有 2000 多个文件夹对象需要搜索。 My desired folder will always begin with an D and be followed by a set of incrementing numbers.我想要的文件夹总是以 D 开头,后面跟着一组递增的数字。 Ex: D1200例如:D1200

The results from the answer led me to creating this call which works:答案的结果使我创建了这个有效的调用:

aws s3 ls main.test.staging/General_Testing/Results/ --recursive | sort | tail -n 1 | awk '{print $4}'

but it takes over 40 secs to search through thousands of files and I then need to regex parse the output to find the folder object and not the last file modified within it.但搜索数千个文件需要 40 多秒,然后我需要正则表达式解析 output 以找到文件夹 object 而不是其中修改的最后一个文件。 Also, if I try to do this to find my desired folder (which is the object right after the Results object):此外,如果我尝试这样做以找到我想要的文件夹(即在Results对象之后的 object):

aws ls s3 main.test.staging/General_Testing/Results/ | sort | tail -1

Then my output will be D998 because the sort function will order folder names like this:然后我的 output 将是D998因为排序 function 将排序文件夹名称如下:

D119
D12
D13

Because technically D12 is greater than D119 because it has a 2 in the 2nd position.因为从技术上讲, D12大于D119 ,因为它在第二个 position 中有一个2 Following this strange logic, there's no way I can use that call to reliable retrieve the highest numbered folder and therefore the last one created.按照这种奇怪的逻辑,我无法使用该调用来可靠地检索编号最高的文件夹,因此是最后一个创建的文件夹。 Something to note is that folder objects that contain files do not have a Last Modified tag that one can use to query.需要注意的是,包含文件的文件夹对象没有可用于查询的Last Modified标记。

I wonder whether you can use a list of CommonPrefixes to overcome your program of having many folders?我想知道您是否可以使用CommonPrefixes列表来克服拥有多个文件夹的程序?

Try this command:试试这个命令:

aws s3api list-objects-v2 --bucket main.test.staging --delimiter '/' --prefix 'General_Testing/Results/' --query CommonPrefixes --output text

(Note that is uses s3api rather than s3 .) (请注意,它使用s3api而不是s3 。)

It should provide a list of 'folders'.它应该提供一个“文件夹”列表。 I don't know whether it has a limit on the number of 'folders' returned.我不知道它是否对返回的“文件夹”数量有限制。

As for sorting D119 before D2 , this is because it is sorting strings.至于在D2之前排序D119 ,这是因为它是对字符串进行排序。 The output is perfectly correct when sorting strings. output 在对字符串进行排序时完全正确。

To sort by the number portion, you can likely use "version sorting".要按数字部分排序,您可能会使用“版本排序”。 See: How to sort strings that contain a common prefix and suffix numerically from Bash?请参阅:如何从 Bash 中对包含公共前缀和后缀的字符串进行数字排序?

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

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