简体   繁体   English

AWS 在 S3 存储桶中找到最大文件大小

[英]AWS find max file size in S3 bucket

I want to find the size and name of the biggest file in my S3 bucket.我想找到我的 S3 存储桶中最大文件的大小和名称。

Currently I have:目前我有:

aws s3api list-objects --bucket bucket-name --output json --query "[max(Contents[].Size), length(Contents[])]"

which does not allow me to see the name of the file.这不允许我看到文件的名称。

I also have the command to list the details of all files on the bucket:我还有命令列出存储桶中所有文件的详细信息:

aws s3api list-object-versions --bucket bucket-name --query 'Versions[*].Size'

What command will give me the name and size of the largest file(s) on the S3 bucket?什么命令会给我 S3 存储桶上最大文件的名称和大小?

Using AWS CLI only, this will find the largest file:仅使用 AWS CLI,这将找到最大的文件:

aws s3api list-objects-v2 --bucket bucket-name --query "sort_by(Contents, &Size)[-1:]"

or to include non-current versions if applicable:或包括非当前版本(如果适用):

aws s3api list-object-versions --bucket bucket-name --query "sort_by(Versions[*], &Size)[-1:]"

Optional tweaks:可选调整:

  • Replace -1 with -N to find the largest N files.-1替换为-N以查找最大的 N 个文件。
  • Include .[Key,Size] at the end of the --query to select only those fields.--query末尾包含.[Key,Size]以仅选择那些字段。

Sadly I think the filtering is done client side because this downloaded 28 MB when run on a large bucket.遗憾的是,我认为过滤是在客户端完成的,因为在大型存储桶上运行时会下载 28 MB。 However it is still a useful 1-liner despite not being quick.然而,尽管速度不快,但它仍然是一个有用的 1-liner。

The following should return the name and size of the largest file in the bucket "bucket-name".以下应返回存储桶“bucket-name”中最大文件的名称和大小。

aws s3api list-object-versions --bucket bucket-name | jq -r '.Versions[] | "\(.Key)\t \(.Size)"' | sort -k2 -r -n | head -1

The command above uses jq which you can install from https://stedolan.github.io/jq/download/上面的命令使用 jq,您可以从https://stedolan.github.io/jq/download/安装它

Here is what I did:这是我所做的:

using ">" operator sent the output of your command to a sizes.txt file.使用“>”运算符将命令的输出发送到 sizes.txt 文件。 Then searched the max size in that text file to find the corresponding filename.然后搜索该文本文件中的最大大小以找到相应的文件名。

steps:脚步:

touch sizes.txt
aws s3api list-object-versions --bucket bucket-name | jq -r '.Versions[] | "\(.Key)\t \(.Size)"' | sort -k2 -r -n > sizes.txt 
vi sizes.txt /"max_size_retrieved_from_command"

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

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