如何使用通配符搜索 Amazon S3 存储桶?
How to search an Amazon S3 Bucket using Wildcards?
This Whosebug answer helped a lot. 但是,我想搜索给定存储桶中的所有 PDF。
- 我点击"None"。
- 开始打字。
- 我输入
*.pdf
- 按
Enter
没有任何反应。有没有办法通过在线 S3 GUI 控制台使用通配符或正则表达式来过滤存储桶搜索结果?
如评论所述,亚马逊的 UI 只能用于根据他们自己的文档按前缀搜索:
http://docs.aws.amazon.com/AmazonS3/latest/UG/searching-for-objects-by-prefix.html
还有其他搜索方法,但它们需要一些努力。仅举两个选项,AWS-CLI application or Boto3 for Python.
我知道这个 post 很旧,但它在 Google 的 s3 搜索列表中排名靠前,并且没有公认的答案。 Harish 的另一个答案是 linking to a dead site。
更新 2020/03/03:上面的 AWS link 已被删除。这是一个与我能找到的非常相似的主题的 link。 https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingKeysHierarchy.html
AWS CLI 搜索:
在 AWS 控制台中,我们只能搜索目录内的对象,但不能搜索整个目录,也只能使用文件的前缀名(S3 搜索限制)。
最好的方法是在 Linux OS
中通过以下命令使用 AWS CLI
aws s3 ls s3://bucket_name/ --recursive | grep search_word | cut -c 32-
正在使用通配符搜索文件
aws s3 ls s3://bucket_name/ --recursive |grep '*.pdf'
如果您在 Python 中使用 boto3,则很容易找到这些文件。将 'bucket' 替换为存储桶的名称。
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket')
for obj in bucket.objects.all():
if '.pdf' in obj.key:
print(obj.key)
您可以使用带有 --dryrun
标志的复制功能:
aws s3 ls s3://your-bucket/any-prefix/ .\ --recursive --exclude * --include *.pdf --dryrun
它将显示所有 PDF 文件。
我在我的一个项目中使用过它,但它有点硬编码
import subprocess
bucket = "Abcd"
command = "aws s3 ls s3://"+ bucket + "/sub_dir/ | grep '.csv'"
listofitems = subprocess.check_output(command, shell=True,)
listofitems = listofitems.decode('utf-8')
print([item.split(" ")[-1] for item in listofitems.split("\n")[:-1]])
使用 Java SDK 的文档表明可以这样做:
https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingKeysHierarchy.html
https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingJava.html
特别是函数 listObjectsV2Result
允许您指定前缀过滤器,例如"files/2020-01-02*" 所以你只能 return 匹配今天日期的结果。
我猜这些文件是从 unix 系统上传的,你下载到 windows 所以 s3cmd 无法保留不适用于 NTFS 的文件权限。
要搜索文件并抓取它们,请从目标目录尝试此操作或将 ./ 更改为目标:
for i in `s3cmd ls s3://bucket | grep "searchterm" | awk '{print }'`; do s3cmd sync --no-preserve $i ./; done
这适用于 windows 的 WSL。
CLI 可以做到这一点; aws s3
只支持前缀,但是aws s3api
支持任意过滤。对于看起来像 s3://company-bucket/category/obj-foo.pdf
、s3://company-bucket/category/obj-bar.pdf
、s3://company-bucket/category/baz.pdf
的 s3 链接,您可以 运行
aws s3api list-objects --bucket "company-bucket" --prefix "category/" --query "Contents[?ends-with(Key, '.pdf')]"
或更通用的通配符
aws s3api list-objects --bucket "company-bucket" --prefix "category/" --query "Contents[?contains(Key, 'foo')]"
甚至
aws s3api list-objects --bucket "company-bucket" --prefix "category/obj" --query "Contents[?ends-with(Key, '.pdf') && contains(Key, 'ba')]"
JMESPath 中描述了完整的查询语言。
This Whosebug answer helped a lot. 但是,我想搜索给定存储桶中的所有 PDF。
- 我点击"None"。
- 开始打字。
- 我输入
*.pdf
- 按
Enter
没有任何反应。有没有办法通过在线 S3 GUI 控制台使用通配符或正则表达式来过滤存储桶搜索结果?
如评论所述,亚马逊的 UI 只能用于根据他们自己的文档按前缀搜索:
http://docs.aws.amazon.com/AmazonS3/latest/UG/searching-for-objects-by-prefix.html
还有其他搜索方法,但它们需要一些努力。仅举两个选项,AWS-CLI application or Boto3 for Python.
我知道这个 post 很旧,但它在 Google 的 s3 搜索列表中排名靠前,并且没有公认的答案。 Harish 的另一个答案是 linking to a dead site。
更新 2020/03/03:上面的 AWS link 已被删除。这是一个与我能找到的非常相似的主题的 link。 https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingKeysHierarchy.html
AWS CLI 搜索: 在 AWS 控制台中,我们只能搜索目录内的对象,但不能搜索整个目录,也只能使用文件的前缀名(S3 搜索限制)。
最好的方法是在 Linux OS
中通过以下命令使用 AWS CLIaws s3 ls s3://bucket_name/ --recursive | grep search_word | cut -c 32-
正在使用通配符搜索文件
aws s3 ls s3://bucket_name/ --recursive |grep '*.pdf'
如果您在 Python 中使用 boto3,则很容易找到这些文件。将 'bucket' 替换为存储桶的名称。
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket')
for obj in bucket.objects.all():
if '.pdf' in obj.key:
print(obj.key)
您可以使用带有 --dryrun
标志的复制功能:
aws s3 ls s3://your-bucket/any-prefix/ .\ --recursive --exclude * --include *.pdf --dryrun
它将显示所有 PDF 文件。
我在我的一个项目中使用过它,但它有点硬编码
import subprocess
bucket = "Abcd"
command = "aws s3 ls s3://"+ bucket + "/sub_dir/ | grep '.csv'"
listofitems = subprocess.check_output(command, shell=True,)
listofitems = listofitems.decode('utf-8')
print([item.split(" ")[-1] for item in listofitems.split("\n")[:-1]])
使用 Java SDK 的文档表明可以这样做:
https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingKeysHierarchy.html https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingJava.html
特别是函数 listObjectsV2Result
允许您指定前缀过滤器,例如"files/2020-01-02*" 所以你只能 return 匹配今天日期的结果。
我猜这些文件是从 unix 系统上传的,你下载到 windows 所以 s3cmd 无法保留不适用于 NTFS 的文件权限。
要搜索文件并抓取它们,请从目标目录尝试此操作或将 ./ 更改为目标:
for i in `s3cmd ls s3://bucket | grep "searchterm" | awk '{print }'`; do s3cmd sync --no-preserve $i ./; done
这适用于 windows 的 WSL。
CLI 可以做到这一点; aws s3
只支持前缀,但是aws s3api
支持任意过滤。对于看起来像 s3://company-bucket/category/obj-foo.pdf
、s3://company-bucket/category/obj-bar.pdf
、s3://company-bucket/category/baz.pdf
的 s3 链接,您可以 运行
aws s3api list-objects --bucket "company-bucket" --prefix "category/" --query "Contents[?ends-with(Key, '.pdf')]"
或更通用的通配符
aws s3api list-objects --bucket "company-bucket" --prefix "category/" --query "Contents[?contains(Key, 'foo')]"
甚至
aws s3api list-objects --bucket "company-bucket" --prefix "category/obj" --query "Contents[?ends-with(Key, '.pdf') && contains(Key, 'ba')]"
JMESPath 中描述了完整的查询语言。