[英]How to display only the directories from s3 bucket using aws cli command
I need to display all the directories present inside the s3 bucket, where some directories are hidden and are shown again only if I filter the version to 'show' instead of hide.我需要显示 s3 存储桶中存在的所有目录,其中一些目录是隐藏的,只有当我将版本过滤为“显示”而不是隐藏时才会再次显示。
The same things goes to files where inside the directory we cant see the file but when we chose the filter in version to 'show', it shows the files.同样的事情也发生在目录中我们看不到文件的文件上,但是当我们在版本中选择过滤器以“显示”时,它会显示文件。
How can we see these files and directories in aws-cli.我们如何在aws-cli中看到这些文件和目录。 Please do help if you have a solution.
如果您有解决方案,请提供帮助。
For example:例如:
roxor@ubuntu:~$ aws s3 ls s3://dw-etl-source-prod
workday dsr
I need to get 4 directories, but since the directories contains versioning it is not been shown.我需要获取 4 个目录,但由于目录包含版本控制,因此未显示。 If i filter in version to show it gives all the files in aws s3 UI, but how I can do the same using AWS CLI.
如果我过滤版本以显示它会在 aws s3 UI 中提供所有文件,但我如何使用 AWS CLI 执行相同的操作。
检查键的最后一个字符/
。
aws s3api list-objects-v2 --bucket <your_bucket> --prefix <prefix> --query 'Contents[?ends_with(Key, `/`)].[Key]' --output text
If you are on a POSIX based system and want to use the familiar aws s3 ls
syntax, try:如果您使用的是基于 POSIX 的系统并且想要使用熟悉的
aws s3 ls
语法,请尝试:
aws s3 ls --recursive <s3Uri> | cut -c32- | xargs -d '\n' -n 1 dirname | uniq
Here's a quick explanation.这是一个快速解释。 As of Nov 2020,
aws s3 ls
prints objects in the following format:截至 2020 年 11 月,
aws s3 ls
以以下格式打印对象:
<date> <time> <size> <path>
The idea is to extract <path>
from this listing using cut
, pass it to dirname
to extract the directory name, and finally use uniq
to avoid repeats.这个想法是使用
cut
从这个列表中提取<path>
,将它传递给dirname
以提取目录名称,最后使用uniq
避免重复。 cut -c32-
trims the s3 listing up to the 31st character. cut -c32-
将 s3 列表修剪到第 31 个字符。 You might have to adjust this if your file sizes are too large.如果您的文件太大,您可能需要调整它。
你可以试试看。
aws s3 ls s3://<bucket-name> --recursive --human-readable --summarize | awk '{print $5}' | awk -F '/' '/\// {print $1}' | sort -u
我的解决方案:
aws s3 ls://<your bucket>/<path>/<to>/ | awk '{print $2}'
I combined a few of these solutions into a simple one liner that works by checking for anything in the directory (but not recursively) that is also a directory (anything with a filename ending in /)我将这些解决方案中的一些组合成一个简单的衬里,该衬里通过检查目录中的任何内容(但不是递归地)来工作,该目录也是一个目录(文件名以 / 结尾的任何内容)
aws s3 ls s3://<your bucket>/<path>/<to>/ | awk '{print $2}' | awk -F '/' '/\// {print $1}'
Hop this helps!希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.