为了列出认知用户池的所有用户,我想到了使用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'
]
)
非常感谢!
答案 0 :(得分:1)
面对同样的问题,对于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