aws 客户端 cognito list_users() 函数的分页替代方法

C.T*_*mas 1 python paginator amazon-cognito boto3

为了列出认知用户池的所有用户,我想到了使用 boto3 的client.list_users()- 功能,包括分页。

不过,如果我打电话print(client.can_paginate('list_users'))False返回,因为该功能list_users()分页。

是否有替代方法可以列出认知用户池的所有用户而不过滤那些已经被选中的用户?

我当前没有分页的代码如下所示:

client = boto3.client('cognito-idp',
                         region_name=aws_region,
                         aws_access_key_id=aws_access_key,
                         aws_secret_access_key=aws_secret_key,
                         config=config)

response = client.list_users(
UserPoolId=userpool_id,
AttributesToGet=[
    'email','sub'
] 
)
Run Code Online (Sandbox Code Playgroud)

提前谢谢了!

Gle*_*leb 5

面对同样的问题,我也很惊讶 Cognito list_user API 没有分页器,所以我构建了这样的东西:

import boto3


def boto3_paginate(method_to_paginate, **params_to_pass):
    response = method_to_paginate(**params_to_pass)
    yield response
    while response.get('PaginationToken', None):
        response = method_to_paginate(PaginationToken=response['PaginationToken'], **params_to_pass)
        yield response



class CognitoIDPClient:
    def __init__(self):
        self.client = boto3.client('cognito-idp', region_name=settings.COGNITO_AWS_REGION)

    ...

    def get_all_users(self):
        """
        Fetch all users from cognito
        """
        # sadly, but there is no paginator for 'list_users' which is ... weird
        # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cognito-idp.html?highlight=list_users#paginators
        users = []
        # if `Limit` is not provided - the api will return 60 items, which is maximum
        for page in boto3_paginate(self.client.list_users, UserPoolId=settings.COGNITO_USER_POOL):
            users += page['Users']
        return users
Run Code Online (Sandbox Code Playgroud)