Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I loop through nextToken

I just kind of figured out how to use the nextToken in boto3. The API call I am making I should expect about 300 entries. I only get 100. I know I need to loop through the next token but I am struggling on how to do that. I am new to the python army.

def myservers():

  response = client.get_servers(maxResults=100,)
  additional = client.get_servers(nextToken=response['nextToken'])

this little snipit will give me the first 50 plus the first 'nextToken' for a total of 100 items. Clearly I need to iterate over and over to get the rest. I am expecting 300 plus items.

like image 845
Randal Avatar asked Jul 17 '26 12:07

Randal


2 Answers

I would just do a simple while loop:

response = client.get_servers()
results = response["serverList"]
while "NextToken" in response:
    response = client.get_servers(NextToken=response["NextToken"])
    results.extend(response["serverList"])
like image 157
Vincent J Avatar answered Jul 28 '26 16:07

Vincent J


I used the suggestion here:

https://github.com/boto/botocore/issues/959#issuecomment-429116381

You have to keep calling client.get_servers() passing in the NextToken.

like image 41
Luke Avatar answered Jul 28 '26 17:07

Luke