I can grab and read all the objects in my AWS S3 bucket via
JavaScript
x
7
1
s3 = boto3.resource('s3')
2
bucket = s3.Bucket('my-bucket')
3
all_objs = bucket.objects.all()
4
for obj in all_objs:
5
pass
6
#filter only the objects I need
7
and then
JavaScript
1
2
1
obj.key
2
would give me the path within the bucket.
Is there a way to filter beforehand for only those files respecting a certain starting path (a directory in the bucket) so that I’d avoid looping over all the objects and filtering later?
Advertisement
Answer
Use the filter
[1], [2] method of collections like bucket.
JavaScript
1
6
1
s3 = boto3.resource('s3')
2
bucket = s3.Bucket('my-bucket')
3
objs = bucket.objects.filter(Prefix='myprefix')
4
for obj in objs:
5
pass
6