使用 ls 和通配符检查文件是否存在于 s3 中

Check if file exists in s3 using ls and wildcard

看起来很简单,但语法不正确。我想知道我的 s3 存储桶中是否存在使用通配符的文件。像

aws s3 ls s3://my-bucket/folder/*myfile*

目标是查看此存储桶中是否存在名为 2016_myfile.txt 的文件或名为 2011_myfile.csv 的文件。

如果我 运行 命令,它不会 return 任何东西,即使我知道这个文件存在于那里。

(根据评论重新起草,因为这似乎回答了问题)

我自己试过,在aws-cli中使用通配符失败,根据docs,目前不支持。 最简单(尽管效率最低)的解决方案是使用 grep:

aws s3 ls s3://my-bucket/folder/ | grep myfile

或者,您可以编写一个简短的 python/other 脚本来更有效地执行此操作(但不是在单个命令中)

2022 年更新:

您可以使用 aws s3 支持的 --dry-run 选项:

aws s3 sync s3://my-bucket . --include "folder/*myfile*" --exclude "*" --dryrun

或者,

s3cmd 也适用于我的 grep。

s3cmd ls --recursive s3://mybucket/folder/folder2 | grep filename

S3 不支持通配符列表。您需要列出所有文件并进行 grep。

aws s3 ls s3://mybucket/folder --recursive 

以上命令会给出文件夹下的文件列表,它也会搜索文件夹内的文件。只需 grep 你的文件名

aws s3 ls s3://mybucket/folder --recursive |grep filename

假设你想查找多个文件,为这些文件创建一个正则表达式并对其进行 grep。

对于 Windows 用户:

aws s3 ls s3://my-bucket/folder/ | findstr myfile

aws s3 ls 不支持 glob,但是同步 支持 并且有干式 运行 模式。所以如果你这样做(从一个空目录)你应该得到你想要的结果:

aws s3 sync s3://my-bucket . --exclude "*" --include "folder/*myfile*" --dryrun

它会为匹配的文件生成这样的行:

(dryrun) download s3://my-bucket/folder/myfile.txt to folder/myfile.txt
(dryrun) download s3://my-bucket/folder/_myfile-foo.xml to folder/_myfile-foo.xml