简体   繁体   English

如何枚举AWS账户中的所有SQS队列

[英]How to enumerate all SQS queues in an AWS account

How do I list all SQS queues in an AWS account programmatically via the API and .Net SDK? 如何通过API和.Net SDK以编程方式列出AWS账户中的所有SQS队列?

I am already doing something similar with DynamoDb tables, and that's fairly straightforward - you can page through results using ListTables in a loop until you have them all. 我已经在使用DynamoDb表做类似的事情了,这非常简单-您可以循环使用ListTables翻页结果,直到获得所有结果为止。

However the equivalent SQS Api endpoint, ListQueues is different and not as useful. 但是,等效的SQS Api端点ListQueues是不同的,没有那么有用。 It returns up to 1000 queues, with no option of paging. 它最多返回1000个队列,没有分页选项。

Yes, there can be over 1000 queues in my case. 是的,我的情况下可能有1000多个队列。 I have had a query return exactly 1000 results. 我有一个查询返回正好1000个结果。 It's all in 1 region, so it's not the same as this question . 全部在1个区域中,因此与这个问题不同

You can retrieve SQS queue names from Cloudwatch, which supports paging. 您可以从支持分页的Cloudwatch中检索SQS队列名称。 It will only return queues that are considered active. 它只会返回被认为是活动的队列。

An active queue is described as : 活动队列描述为

A queue is considered active by CloudWatch for up to six hours from the last activity (for example, any API call) on the queue. 从队列的上一个活动(例如,任何API调用)起,CloudWatch认为该队列处于活动状态的时间长达六个小时。

Something like this should work: 这样的事情应该起作用:

var client = new AmazonCloudWatchClient(RegionEndpoint.EUWest1);
string nextToken = null;
var results = Enumerable.Empty<string>();

do
{
    var result = client.ListMetrics(new ListMetricsRequest()
    {
        MetricName = "ApproximateAgeOfOldestMessage",
        NextToken = nextToken
    });

    results = results.Concat(
        result
        .Metrics
        .SelectMany(x => x.Dimensions.Where(d => d.Name == "QueueName")
        .Select(d => d.Value))
    );

    nextToken = result.NextToken;

} while (nextToken != null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM