gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:15:33 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com