locked
List all blobs in C# RRS feed

  • Question

  • Hello, 

    I'm trying to get a list of all blobs in a storage container using C# but my code is only returning blobs that are in the root, not the ones that are in virtual folders.  I know they are not real folders but they show up in Azure storage explorer as folders. 

    Any idea what I need to do in order to get the 79 "files" that are in "aaa\05\log"? 

     var connectionString = @"myConStr";
    
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference("aaa");
    
                foreach (IListBlobItem item in container.ListBlobs(null, false, BlobListingDetails.None)
                {
                    if (item.GetType() == typeof(CloudBlockBlob))
                    {
                        CloudBlockBlob blob = (CloudBlockBlob)item;
                        Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);
                    }
                    else if (item.GetType() == typeof(CloudPageBlob))
                    {
                        CloudPageBlob pageBlob = (CloudPageBlob)item;
                        Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);
                    }
                    else if (item.GetType() == typeof(CloudBlobDirectory))
                    {
                        CloudBlobDirectory directory = (CloudBlobDirectory)item;
                        Console.WriteLine("Directory: {0}", directory.Uri);
                    }
                }
    These are shown

    However these are not

    Wednesday, December 13, 2017 3:46 PM

Answers

  • Ah, I think it's this. I needed to cast the directory and then call .ListBlobs on that. 

    Is this the best/only way?  

     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference("aaa");
    
                foreach (IListBlobItem blobItem in container.ListBlobs())
                {
                    if (blobItem is CloudBlobDirectory)
                    {
                        CloudBlobDirectory directory = (CloudBlobDirectory)blobItem;
                        IEnumerable<IListBlobItem> blobs = directory.ListBlobs(true);
                        ICloudBlob bi;
                        foreach (var blob in blobs)
                        {
                            if (blob is CloudPageBlob)
                            {
                                bi = blob as CloudPageBlob;
                                Console.WriteLine(bi.Name);
                                Console.WriteLine(bi.Properties.LastModified.ToString());
                                Console.WriteLine();
                                Console.WriteLine(@"==========================");
                            }
                        }
                    }
                }

    Wednesday, December 13, 2017 3:59 PM

All replies

  • Ah, I think it's this. I needed to cast the directory and then call .ListBlobs on that. 

    Is this the best/only way?  

     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference("aaa");
    
                foreach (IListBlobItem blobItem in container.ListBlobs())
                {
                    if (blobItem is CloudBlobDirectory)
                    {
                        CloudBlobDirectory directory = (CloudBlobDirectory)blobItem;
                        IEnumerable<IListBlobItem> blobs = directory.ListBlobs(true);
                        ICloudBlob bi;
                        foreach (var blob in blobs)
                        {
                            if (blob is CloudPageBlob)
                            {
                                bi = blob as CloudPageBlob;
                                Console.WriteLine(bi.Name);
                                Console.WriteLine(bi.Properties.LastModified.ToString());
                                Console.WriteLine();
                                Console.WriteLine(@"==========================");
                            }
                        }
                    }
                }

    Wednesday, December 13, 2017 3:59 PM
  • Thank you for sharing your solution and experience here. It will be greatly beneficial for other community members who have similar questions.

    Do click on "Mark as Answer" on the post that helps you, this can be beneficial to other community members.

    Thursday, December 14, 2017 4:22 PM