python 3.x - How to use Boto3 pagination -


background:

the aws operation list iam users returns max of 50 default.

reading docs (links) below ran following code , returned complete set data setting "maxitems" 1000.

paginator = client.get_paginator('list_users') response_iterator = paginator.paginate(  paginationconfig={      'maxitems': 1000,      'pagesize': 123}) page in response_iterator:     u = page['users']     user in u:         print(user['username']) 

http://boto3.readthedocs.io/en/latest/guide/paginators.html https://boto3.readthedocs.io/en/latest/reference/services/iam.html#iam.paginator.listusers

question:

if "maxitems" set 10, example, best method loop through results? the

i tested following loops 2 iterations before 'istruncated' == false , results in "keyerror: 'marker'". not sure why happening because know there on 200 results.

marker = none  while true:     paginator = client.get_paginator('list_users')     response_iterator = paginator.paginate(          paginationconfig={             'maxitems': 10,             'startingtoken': marker})     #print(response_iterator)     page in response_iterator:         u = page['users']         user in u:             print(user['username'])             print(page['istruncated'])             marker = page['marker']             print(marker)         else:             break 

thanks.

(answer rewrite) **note **, paginator contains bug doesn't tally documentation (or vice versa). maxitems doesn't return marker or nexttoken when total items exceed maxitems number. indeed pagesize 1 controlling return of marker/nexttoken indictator.

import sys import boto3 iam = boto3.client("iam") marker = none while true:     paginator = iam.get_paginator('list_users')     response_iterator = paginator.paginate(          paginationconfig={             'pagesize': 10,             'startingtoken': marker})     page in response_iterator:         print("next page : {} ".format(page['istruncated']))         u = page['users']         user in u:             print(user['username'])     try:         marker = page['marker']         print(marker)     except keyerror:         sys.exit() 

it not mistake code doesn't works. maxitems in paginator seems become "threshold" indicator. ironically, maxitems inside original boto3.iam.list_users still works mentioned.

if check boto3.iam.list_users, notice either omit marker, otherwise must put value. apparently, paginator not wrapper boto3 class list_* method.

import sys import boto3 iam = boto3.client("iam") marker = none while true:     if marker:         response_iterator = iam.list_users(             maxitems=10,             marker=marker         )     else:         response_iterator = iam.list_users(             maxitems=10         )     print("next page : {} ".format(response_iterator['istruncated']))     user in response_iterator['users']:         print(user['username'])      try:         marker = response_iterator['marker']         print(marker)     except keyerror:         sys.exit() 

you can follow the issue filed in boto3 github. according member, can call build_full_result after paginate(), show desire behavior.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -