Paginating List Cognito Identities in AWS PHP SDK v3

500 views Asked by At

How do I fetch all records when the result is paginated, using the AWS PHP SDK v3? I have the following code:

require_once 'vendor/autoload.php';

$cognitoIdentityClient = new Aws\CognitoIdentity\CognitoIdentityClient([
    'region' => 'eu-west-1',
    'version' => '2014-06-30',
    'credentials' => [
        'key' => '**************',
        'secret' => '***************',
    ],
]);

$identities = $cognitoIdentityClient->getPaginator('ListIdentities', [
    'IdentityPoolId' => 'eu-west-1:****************************',
]);

which looks like it should work, but produces the error:

Fatal error: Uncaught UnexpectedValueException: There is no ListIdentities paginator defined for the cognito-identity service. in /path/to/vendor/aws/aws-sdk-php/src/Api/Service.php:363
Stack trace:
#0 /path/to/vendor/aws/aws-sdk-php/src/AwsClientTrait.php(23): Aws\Api\Service->getPaginatorConfig('ListIdentities')
#1 /path/to/report.php(24): Aws\AwsClient->getIterator('ListIdentities', Array)
#2 {main}
  thrown in /path/to/vendor/aws/aws-sdk-php/src/Api/Service.php on line 363

The getPaginator method exists, but the file data/cognito-identity/2014-06-30/paginators-1.json.php is blank, so no paginators are implemented. I see NextToken in the response, but don't understand the pattern to seamlessly load more results (do (...) {} while (...)?)

1

There are 1 answers

0
PeterB On

I solved it like this:

$identities = [];
$i = $cognitoIdentityClient->listIdentities([
    'IdentityPoolId' => IDENTITYPOOLID,
    'MaxResults' => 60,
]);
$identities = array_merge($identities, $i->get('Identities'));
while ($nextToken = $i->get('NextToken')) {
    $i = $cognitoIdentityClient->listIdentities([
        'IdentityPoolId' => IDENTITYPOOLID,
        'MaxResults' => 60,
        'NextToken' => $nextToken,
    ]);
    $identities = array_merge($identities, $i->get('Identities'));
}