简体   繁体   中英

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.

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. 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. 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 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:

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:

<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 -c32- trims the s3 listing up to the 31st character. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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