Boto3: capture only selected objects from S3 resource

I can capture and read all objects in my AWS S3 bucket through

s3 = boto3.resource('s3')
    bucket = s3.Bucket('my-bucket')
    all_objs = bucket.objects.all()
    for obj in all_objs:
        pass
        #filter only the objects I need

and then

obj.key

will give me a way in a bucket.

Is there a way to pre-filter only those files that belong to a specific start path (the directory in the bucket) so that I can avoid traversing all objects and filtering later?

+4
source share
1 answer

Use filter [1] , [2] for collections such as a bucket.

s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
objs = bucket.objects.filter(Prefix='/myprefix')
for obj in objs:
    pass
+10
source

All Articles