我有一个s3桶mybucket,它包含以下目录结构中的三个文件
a/b/c/d/some_file.txt
a/b/d/d/some_file2.txt
x/y/z/yet_another_file.txt我可以使用以下方法列出所有文件:
import boto3
# Extract the files from the s3 bucket
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
bucket_files = [x.key for x in bucket.objects.all()]尽管这将产生s3桶中的所有文件,例如:
a/b/c/d/some_file.txt
a/b/d/d/some_file2.txt
x/y/z/yet_another_file.txt我怎么能只列出a中的文件呢?例如:
a/b/c/d/some_file.txt
a/b/d/d/some_file2.txt发布于 2016-05-19 06:26:01
将filter与Prefix结合使用
import boto3
# Extract the files from the s3 bucket
s3 = boto3.resource('s3')
bucket = s3.Bucket('mybucket')
bucket_files = [x.key for x in bucket.objects.filter(Prefix='a/')]发布于 2017-02-05 06:45:29
另一种选择:
import boto3
s3 = boto3.client('s3')
resp = s3.list_objects_v2(Bucket='mybucket', Prefix='a')https://stackoverflow.com/questions/37314923
复制相似问题