Vicky
Vicky

Reputation: 734

Get access to all queues in Azure Storage Account

I am using Azure.Storage.Queues package as per the recommendation on Microsoft docs. I am connecting to queue using Queue Client something like below,

public class QueueClientSingleton : IQueueClientSingleton
{
    private readonly Lazy<QueueClient> _queueClientXyz;
    private readonly IConfiguration _configuration;

    public QueueClientSingleton(IConfiguration configuration)
    {
        _configuration = configuration;

        _queueClientXyz = new Lazy<QueueClient>(() => new QueueClient(_configuration.ConnectionString, "QueueXyz");
    }
}

The above code is not tested but should work to provide access to QueueXyz. I have a use case wherein I want to connect to the storage account via connection string once and then

  1. read thru different queues (pass queue name and get messages from that queue)
  2. list all queues are present in that storage account

I found something like below but this is using the deprecated package: Microsoft.WindowsAzure.Storage that we are not using in our solution. Is there something we can do using similarly with a new package: Azure.Storage.Queues

public static void CreateKnownAzureQueues(string azureConnectionString)
{
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azureConnectionString);
    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

    foreach (var queueName in AzureQueues.KnownQueues)
    {
        queueClient.GetQueueReference(queueName).CreateIfNotExists();
    }
}

Also, found below the link to request a list of all queues in the storage account. But, It does not provide any example as such.
https://learn.microsoft.com/en-us/rest/api/storageservices/list-queues1#request

Upvotes: 0

Views: 1157

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29940

When using the Azure.Storage.Queues package, you can use the code below to achieve your purpose:

        QueueServiceClient serviceClient = new QueueServiceClient(conn_str);
        
        //list all queues in the storage account
        var myqueues = serviceClient.GetQueues().AsPages();

        //then you can write code to list all the queue names          
        foreach (Azure.Page<QueueItem> queuePage in myqueues)
        {
            foreach (QueueItem q in queuePage.Values)
            {
                Console.WriteLine(q.Name);
            }

        }

        //get a queue client
        var myqueue_client = serviceClient.GetQueueClient("the queue name");

Upvotes: 2

Related Questions