如何只显示来自aws s3 ls命令的文件?

问题描述 投票:33回答:7

我正在使用aws cli使用以下命令(documentation)列出s3存储桶中的文件:

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

这个命令给我以下输出:

2013-09-02 21:37:53   10 Bytes a.txt
2013-09-02 21:37:53  2.9 MiB foo.zip
2013-09-02 21:32:57   23 Bytes foo/bar/.baz/a
2013-09-02 21:32:58   41 Bytes foo/bar/.baz/b
2013-09-02 21:32:57  281 Bytes foo/bar/.baz/c
2013-09-02 21:32:57   73 Bytes foo/bar/.baz/d
2013-09-02 21:32:57  452 Bytes foo/bar/.baz/e
2013-09-02 21:32:57  896 Bytes foo/bar/.baz/hooks/bar
2013-09-02 21:32:57  189 Bytes foo/bar/.baz/hooks/foo
2013-09-02 21:32:57  398 Bytes z.txt

Total Objects: 10
   Total Size: 2.9 MiB

但是,这是我想要的输出:

a.txt
foo.zip
foo/bar/.baz/a
foo/bar/.baz/b
foo/bar/.baz/c
foo/bar/.baz/d
foo/bar/.baz/e
foo/bar/.baz/hooks/bar
foo/bar/.baz/hooks/foo
z.txt

如何省略日期,时间和文件大小以仅显示文件列表?

linux amazon-web-services amazon-s3 ls aws-cli
7个回答
53
投票

您不能仅使用aws命令执行此操作,但您可以轻松地将其传递给另一个命令以去除您不想要的部分。您还需要删除--human-readable标志以使输出更容易使用,并使用--summarize标志删除最后的摘要数据。

试试这个:

aws s3 ls s3://mybucket --recursive | awk '{print $4}'

编辑:将文件名中的空格考虑在内:

aws s3 ls s3://mybucket --recursive | awk '{$1=$2=$3=""; print $0}' | sed 's/^[ \t]*//'

7
投票

一个简单的过滤器将是:

aws s3 ls s3://mybucket --recursive | perl -pe 's/^(?:\S+\s+){3}//'

这将删除日期,时间和大小。只留下文件的完整路径。它也可以在没有递归的情况下工作,它也应该与包含空格的文件名一起使用。


6
投票

使用s3api和jq(AWS docu aws s3api list-objects):

此模式始终是递归的。

$ aws s3api list-objects --bucket "bucket" | jq -r '.Contents[].Key'
a.txt
foo.zip
foo/bar/.baz/a
[...]

您可以通过添加前缀(此处为foo目录)来过滤子目录。前缀不能以/开头。

$ aws s3api list-objects --bucket "bucket" --prefix "foo/" | jq -r '.Contents[].Key'
foo/bar/.baz/a
foo/bar/.baz/b
foo/bar/.baz/c
[...]

jq选项:

  • -r =原始模式,输出中没有引号
  • .Contents[] =获取Contents对象数组内容
  • .Key =获取每个关键字段(不生成有效的JSON数组,但我们处于原始模式,所以我们不关心)

附录:

您可以使用纯AWS CLI,但值将由qazxsw poi = Horizo​​ntal Tab(qazxsw poi)分隔

\x09

AWS CLI选项:


3
投票

简单的方法

--query "Contents[].Key"

1
投票

简单的命令就是

--output text

如果需要时间戳,只需更新命令字段值即可。


1
投票

对于文件名,我发现最容易:

aws s3 ls s3://mybucket --recursive --human-readable --summarize|cut -c 29-

这将在空格(aws s3 ls s3://mybucket --recursive --human-readable --summarize |cut -d ' ' -f 8 )处剪切返回的输出并返回第四列(aws s3 ls s3://path/to/bucket/ | cut -d " " -f 4),这是文件名列表。


1
投票

我的解决方案

使用aws cli递归列出文件。

cut -d " "

-f 4 - 清除空行。


示例:aws s3 ls s3://myBucket --recursive | awk 'NF>1{print $4}' | grep .

grep .

解决方案:aws s3 ls s3://myBucket

                           PRE f5c10c1678e8484482964b8fdcfe43ad/
                           PRE f65b94ad31734135a61a7fb932f7054d/
                           PRE f79b12a226b542dbb373c502bf125ffb/
                           PRE logos/
                           PRE test/
                           PRE userpics/
2019-05-14 10:56:28       7754 stage.js
© www.soinside.com 2019 - 2024. All rights reserved.