locked
Azure storage - ListContainers only returning single row RRS feed

  • Question

  • Hello, 

    I have an Azure storage account that has approx. 30 blob containers in it.  I'm trying to get a list of all these containers in a .net 4.5 console app without much luck. 

    var lisfOfContainers = new List<string>();
    
    storageAccount = CloudStorageAccount.Parse(ae.ConnectionString);
    blobClient = storageAccount.CreateCloudBlobClient();
    IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers();
    
    foreach (CloudBlobContainer c in containers)
    {
        lisfOfContainers.Add(c.Uri.Segments[2] != null ? c.Uri.Segments[2].ToString() : string.Empty);
    }
    
    return lisfOfContainers;

    Firstly the call to ListContainers only returns one result, even if I enumerate the result set in debug and secondly, the foreach bombs out on on the array element 2. 

    Does anyone have a working example of how to do this they could share? 

    Thanks

    Saturday, April 28, 2018 10:15 PM

Answers

  • Posting solution for people searching in future. 

    void Main()
    {
      var storageAccount = CloudStorageAccount.Parse(@"########");
      var blobClient = storageAccount.CreateCloudBlobClient();
      ListAllContainers(blobClient, string.Empty); 	 
    }
    
    private static void ListAllContainers(CloudBlobClient blobClient, string prefix)
    {
    	try
    	{
    
    		foreach (var container in blobClient.ListContainers(prefix, ContainerListingDetails.None, null, null))
    		{
    			Console.WriteLine(container.Name);
    		}
    
    	}
    	catch (StorageException e)
    	{
    		Console.WriteLine(e.Message);
    		Console.ReadLine();
    		throw;
    	}
    }

    • Marked as answer by vikranth s Thursday, May 3, 2018 4:32 PM
    Thursday, May 3, 2018 4:22 PM

All replies

  • Refer to this link for sample on Azure blob container list, and see if it helps.

    • Proposed as answer by vikranth s Wednesday, May 2, 2018 7:23 PM
    Monday, April 30, 2018 9:45 AM
  • Posting solution for people searching in future. 

    void Main()
    {
      var storageAccount = CloudStorageAccount.Parse(@"########");
      var blobClient = storageAccount.CreateCloudBlobClient();
      ListAllContainers(blobClient, string.Empty); 	 
    }
    
    private static void ListAllContainers(CloudBlobClient blobClient, string prefix)
    {
    	try
    	{
    
    		foreach (var container in blobClient.ListContainers(prefix, ContainerListingDetails.None, null, null))
    		{
    			Console.WriteLine(container.Name);
    		}
    
    	}
    	catch (StorageException e)
    	{
    		Console.WriteLine(e.Message);
    		Console.ReadLine();
    		throw;
    	}
    }

    • Marked as answer by vikranth s Thursday, May 3, 2018 4:32 PM
    Thursday, May 3, 2018 4:22 PM
  • Thanks for sharing this information. It should certainly help those with a scenario like yours.

    Thursday, May 3, 2018 4:32 PM