【问题标题】:Cognito ListUsers continues foreverCognito ListUsers 永远持续下去
【发布时间】:2019-03-23 17:53:12
【问题描述】:

嘿,我写了一些代码来下载我的 Cognito 用户池中的所有用户,但它似乎永远持续下去。我很确定我正确使用了分页标记。 Cognito 的 UI 估计我有大约 10,000 个用户,但是我在循环中放置了一个断点分钟,而我的 List 有超过 50,000 个用户,这没有任何意义。

非常感谢您的帮助!

        using (AmazonCognitoIdentityProviderClient provider = AuthorizedClient())
        {
            try
            {
                List<UserType> users = new List<UserType>();
                bool continueListing = true;

                ListUsersResponse response = provider.ListUsers(
                    new ListUsersRequest
                    {
                        UserPoolId = UserPoolId,
                        Limit = 60,
                        AttributesToGet = new List<string>
                        {
                            "email"
                        }
                    });

                users.AddRange(response.Users);
                string paginationToken = response.PaginationToken;
                while (continueListing)
                {
                    response = provider.ListUsers(
                        new ListUsersRequest
                        {
                            UserPoolId = UserPoolId,
                            Limit = 60,
                            PaginationToken = paginationToken,
                            AttributesToGet = new List<string>
                            {
                                "email"
                            }
                        });

                    if (response.Users.Count < 60)
                    {
                        continueListing = false;
                    }
                    else
                    {
                        paginationToken = response.PaginationToken;
                        users.AddRange(response.Users);
                    }
                }

                return users;
            }
            catch (Exception)
            {
                //_session.NotifyUser(Notification.GeneralError());
                //_logger.LogError(e.Message);
            }

            return null;
        }

【问题讨论】:

  • users.Count() 为50,000 时,users.Select(z =&gt; z.SomePropertyThatIsTheUserID).Distinct().Count() 返回什么?

标签: c# amazon-web-services amazon-cognito


【解决方案1】:

不要列出来自 cognito 的用户! 您将开始达到他们的 api 限制。 为您的用户轮询您的数据库,您拥有更多控制权。

【讨论】:

    【解决方案2】:

    我最终使用费率门让它工作。我认为当您发送垃圾邮件请求时,HTTP 世界中的某些内容会变得混乱,并且您会开始收到有趣的响应。无论如何,如果您每秒超过 5 次 ListUsers API 调用,您将得到一个 RateExceedException 并且无论如何它都不会工作,所以无论如何我都需要一个速率门。

    这是我使用的“RateGate”,效果非常好。我只是将其设置为将我的循环限制为每秒 4 次迭代(比最大值少 1 次)。

    https://github.com/Danthar/RateLimiting

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      相关资源
      最近更新 更多