简体   繁体   English

如何使用 aws cli 命令仅显示 s3 存储桶中的目录

[英]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.

相关问题 如何使用 PHP 在我的网站上显示来自 AWS S3 存储桶的图像或视频文件 - How to display image or video file from AWS S3 bucket on my website using PHP 如何使用 AWS CLI 仅复制 S3 存储桶中与给定字符串模式匹配的文件 - How to use AWS CLI to only copy files in S3 bucket that match a given string pattern 如何在不使用 AWS CLI 删除现有标签的情况下将标签添加到 S3 存储桶? - How to add tags to an S3 Bucket without deleting the existing tags using AWS CLI? 有没有办法使用 aws s3 ls cli 将 S3 存储桶名称添加到存储桶的递归列表中? - Is there a way to add the S3 bucket name to the recursive list of a bucket using aws s3 ls cli? 如何使用 CLI 删除 AWS S3 中的版本化存储桶? - How do I delete a versioned bucket in AWS S3 using the CLI? 需要在 Ubuntu 22.04 上使用 AWS CLI 将所有文件从特定本地目录上传/复制到 AWS S3 存储桶 - Need to upload/copy ALL files from specific local directory to AWS S3 bucket using AWS CLI on Ubuntu 22.04 如何使用 aws-sdk-2.x 从 S3 存储桶中获取 object 的 S3 URL - How to get S3 URL for the object from S3 bucket using aws-sdk-2.x 从一个存储桶到另一个存储桶的 AWS S3 同步命令 - AWS S3 sync command from one bucket to another 如何仅使用 aws codebuild 服务将 github 代码复制到 s3 存储桶? - How to copy github code to s3 bucket using only aws codebuild service? 无法通过传输连接到 S3 存储桶,只能通过 AWS CLI - Cannot connect to S3 bucket via Transmit, only via AWS CLI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM