0

I wanted to create folders and sub-folders, I found this workaround: but when I listed them: using this code (source):

foreach (IListBlobItem item in Container.ListBlobs(null, false))
            {
                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);
                }
            }

It only shows parent folders and blobs in the root container. I was expecting to get them all as blobs since this is virtual directory not real, for example I have this file

https://account.blob.core.windows.net/container/Accounts/Images/1/acc.jpg

but it doesn't show, it just show:

https://account.blob.core.windows.net/container/Accounts

and

https://account.blob.core.windows.net/container/anyfile

Do I have to request sub-folders inside parent folders to reach the file?

Community
  • 1
  • 1
mshwf
  • 7,009
  • 12
  • 59
  • 133

2 Answers2

4

Please try by specifying 2nd parameter as true in ListBlobs method. This parameter indicates if you want flat blob listing (true) or hierarchical blob listing (false).

From the documentation link:

useFlatBlobListing

Type: System.Boolean

A boolean value that specifies whether to list blobs in a flat listing, or whether to list blobs hierarchically, by virtual directory.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
0

If someone is looking for such flat file listing with the latest Azure.Storage.Blobs SDK, this is how you do it:

BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient(resource.Name);
var blobItemList = blobContainerClient.GetBlobs(); // This will get blobs without any hierarchy
Deepak
  • 2,660
  • 2
  • 8
  • 23