是否可以限制 S3 存储桶中递归目录列表的深度?

Mar*_*kTO 8 recursion ls amazon-s3

我使用了以下命令:

aws s3 ls s3://mybucket/mydir --recursive > bigfile
Run Code Online (Sandbox Code Playgroud)

生成的文件太大(9.5MB),无法方便地使用,因为我需要仔细查看我正在寻找的信息。

我真正需要的只是三层以下的信息。是否可以调整此命令,以便我只向下递归 N 个级别,而不是一直向下递归每个目录?我没有看到任何类似于-maxdepthS3 CLI ls 命令的内容

更新:这是我最终用来获取所需信息的命令,尽管我对此不满意。当我只想要 40 个左右的唯一值时,它仍然给了我 77000 个结果,但它足够短,可以移植到 Excel 中,并通过文本到列进行缩减并删除重复项。

 aws s3 ls s3://mybucket/mydir --human-readable --summarize --recursive | egrep '*_keytext_*' | tr -s ' ' | cut -d' ' -f5 >smallerfile
Run Code Online (Sandbox Code Playgroud)

dsz*_*dsz 6

虽然接受的答案完全正确,但拥有此功能仍然非常有用,aws-cli 上的错误报告证明了这一点 ( https://github.com/aws/aws-cli/issues/2683 )。

我用一个bash脚本和一个awk脚本解决了这个问题。bash 脚本获取单个级别,awk脚本解析输出并将递归调用bash脚本以获得下一个级别。

#!/bin/bash
# Save as ./s3-tree.sh
bucket=$1; max_depth=$2; path=${3:-}; depth=${4:-1};
[ $depth -gt $max_depth ] || \
  aws s3 ls "s3://$bucket/$path" | \
  awk -v bucket="$bucket" -v path="$path" -v depth="$depth" -v max_depth="$max_depth" -f s3-tree.awk
Run Code Online (Sandbox Code Playgroud)
#!/bin/awk
# Save as: ./s3-tree.awk
BEGIN  { FIELDWIDTHS = "10 1 8 1 10 1 600" }
$5 == 0 { next } # Ignore zero-size files
{ print $1 " " $3 " " $5 " " path $7 }
$5 == "       PRE" && depth <= max_depth { system("./s3-tree.sh " bucket " " max_depth " " path $7 " " depth+1); next }
Run Code Online (Sandbox Code Playgroud)

调用为:

./s3-tree.sh <my-bucket-name> <max-depth> [<starting-path>]
Run Code Online (Sandbox Code Playgroud)

分享并享受!


Joh*_*ein 3

Amazon S3 没有“级别”的概念。它是一个平面存储系统,路径是对象名称(Key)的一部分。然而,某些 API 调用支持指定 的功能Prefix,其操作就像在特定目录中查找一样。

另一种方法aws s3 ls是使用Amazon S3 Inventory,它可以提供列出存储桶内容的每日 CSV 文件。