简体   繁体   English

如何按特定文件大小获取查询 s3 存储桶

[英]How to get query s3 bucket by specific file size

at the moment there's some files being uploaded where they are getting corrupted.目前有一些文件正在上传,但它们已损坏。 They'll have a filesize of 0 bytes.它们的文件大小为 0 字节。 May I ask how do I query my s3 bucket and filter by specific size, i'm trying to query when byte is 0?请问如何查询我的 s3 存储桶并按特定大小过滤,我正在尝试查询字节为 0 时?

在此处输入图像描述

At the moment I have two queries.目前我有两个疑问。

First one list all the files recursively in the bucket but no sorting.第一个以递归方式列出存储桶中的所有文件,但不进行排序。

aws s3 ls s3://testbucketname --recursive --summarize --human-readable

Second one sorts but only when provided a prefix, in my case the prefix is the folder name.第二种排序,但只有在提供前缀时才会排序,在我的例子中,前缀是文件夹名称。 My current bucket structure is as followed {accountId}/{filename}我当前的存储桶结构如下{accountId}/{filename}

aws s3api list-objects-v2 --max-items 10 --bucket testbucketname --prefix "30265"  --query "sort_by(Contents,&Size)"

30265 is the accountId/folder name. 30265 是帐户 ID/文件夹名称。 When the prefix isn't provided, the sort doesn't quite work.当没有提供前缀时,排序就无法正常工作。

Any help would be greatly appreciated.任何帮助将不胜感激。

This query works well for filtering the name which is a string此查询适用于过滤字符串名称

aws s3api list-objects --bucket testbucketname --query "Contents[?contains(Key, '.jpg')]" aws s3api list-objects --bucket testbucketname --query "Contents[?contains(Key, '.jpg')]"

Unfortunately I couldn't use contains for Size and there isn't a equals.不幸的是,我不能对 Size 使用 contains 并且没有相等的。

You can use the --query logic to filter the list objects locally to only those that are zero-byte big:您可以使用--query逻辑在本地将列表对象过滤为仅零字节大的对象:

aws s3api list-objects-v2 --bucket example-bucket --query 'Contents[?Size==`0`]'

Or, if you just want to see the list of keys without other meta-data, you can further filter the list:或者,如果您只想查看没有其他元数据的键列表,您可以进一步过滤列表:

aws s3api list-objects-v2 --bucket example-bucket --query 'Contents[?Size==`0`].Key'

(For both of these, replace the outer ' with " when running on Windows.) (对于这两个,当在 Windows 上运行时,将外部'替换为" 。)

Further, if the goal is the remove these objects, you can use jq and a subshell to construct a query that deletes the targeted objects:此外,如果目标是删除这些对象,则可以使用 jq 和子 shell 构造一个删除目标对象的查询:

aws s3api delete-objects --bucket example-bucket --delete \
"$(aws s3api list-objects-v2 --bucket example-bucket --query 'Contents[?Size==`0`].Key' |\
 jq '{"Objects": map({"Key":.})}')"

There isn't a direct way to do this same sort of construct with Windows's command interpreter.没有一种直接的方法可以用 Windows 的命令解释器来完成这种相同类型的构造。

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

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