Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the recommended way of checking if a Paginator is empty? #1824

Closed
l1x opened this issue Sep 12, 2019 · 3 comments
Closed

What is the recommended way of checking if a Paginator is empty? #1824

l1x opened this issue Sep 12, 2019 · 3 comments
Assignees
Labels

Comments

@l1x
Copy link

l1x commented Sep 12, 2019

I am trying to improve on this code:

def list_objects_in_s3(s3_client, s3_bucket, s3_object_prefix):
    paginator = s3_client.get_paginator('list_objects')
    filters = { 'Bucket': s3_bucket,
                'Prefix': s3_object_prefix }
    page_iterator = paginator.paginate(**filters)
    ret = []
    for page in page_iterator:
        if page.get('Contents',None):
            for content in page.get('Contents'):
                ret.append(content['Key'])
    return ret

Is there a way to do this better? I guess I could just return the paginator to the caller and iterate over there. Maybe use yield? I hope that somebody has some idiomatic way of doing this.

@swetashre swetashre self-assigned this Sep 12, 2019
@swetashre swetashre added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Sep 13, 2019
@swetashre
Copy link
Contributor

Thank you for your post. The code you provided will return a empty list if the paginator does not contain any items. You can use can_paginate if you want to know whether the operation is pageable or not.

Hope it helps and please let me know if you are asking for something else or i am understanding your question wrong.

@l1x
Copy link
Author

l1x commented Sep 17, 2019

Thanks @swetashre this is exactly I was looking for. I wanted to collapse the following to a nested list comprehension but that fails. With can_paginate I can make it work though.

    for page in page_iterator:
        if page.get('Contents',None):
            for content in page.get('Contents'):
                ret.append(content['Key'])

@l1x l1x closed this as completed Sep 17, 2019
@djsmith42
Copy link

This might help. I noticed that each page has a KeyCount key, which tells you how many S3 objects are contained in each page. So if the first page from the paginator has a KeyCount of 0, then you know it's empty.

Like this:

paginator = client.get_paginator('list_objects_v2')
for page in paginator.paginate(Bucket='my-bucket', Prefix='my-prefix'):
  if page['KeyCount'] == 0:
    continue # empty page (should be the only page in the paginator)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants