AWS S3 object filter to NOT match Prefix in Python script

When iterating over S3 objects using Python/boto3, I see that there's a filter method. But can you apply a NOT condition?

I want to just get the top level objects, not objects in folders (they have a prefix). I am currently doing this and it works:

import boto3

s3 = boto3.resource('s3')
bucket = cfg['s3']['bucket_name']

for obj in s3.Bucket(bucket).objects.all():
    if not re.match('folder_name.*', obj.key):

I see support for a filter like this:

for obj in s3.Bucket(bucket).objects.filter(Prefix=folder_name):

I'm asking is there a way to say Prefix != folder_name?


Solution 1:

If you just want a list of objects at without a shared prefix, specify the delimiter to the filter, and boto3 will filter away the shared prefixes:

s3 = boto3.resource('s3')
for obj in s3.Bucket(bucket).objects.filter(Delimiter='/'):
    print(obj.key)