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

标签 python boto3 amazon-cognito paginator

为了列出认知用户池的所有用户,我想到使用 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'
] 
)

非常感谢!

最佳答案

面对同样的问题,也很惊讶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

关于python - aws 客户端 cognito list_users() 函数的分页替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54388296/

相关文章:

python - 模拟dict接口(interface)python

amazon-web-services - 如何在 s3 上分布式获取 key 列表

facebook - 在 aws 中使用 facebook 登录时,用户没有在用户池中创建? - 安卓

amazon-web-services - 如何允许未经身份验证的用户(猜测)访问 aws appsync?

python - 如何使用 Boto3 (Python) 列出可用区域

amazon-web-services - AWS Cognito 角色 : Distinguish between Federated Identity Pool roles and User Pool Group roles

python - 在 Ubuntu : pip not working 上安装 Python 3.10

python - 在脚本崩溃之前调用函数?

python - 跨多个表搜索(最佳实践)

python - 预期关键数据类型不匹配 : S actual: L Dynamodb insert error with boto3