Container.ListBlobs gives a list of CloudBlobDirectory. Did I expect a list of CloudBlockBlobs?

I use container.ListBlobs, but it seems to return a list of {Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.LazyEnumerable}, however, when I do the foreach, the object seems to be CloudBlobDirectory, not a CloudBlockBlobs list. Am I doing something wrong, or is this what he should return? Is there some way that I can just get a list of blobs and not blobdirectories?

var storageAccount = CloudStorageAccount.Parse(conn); var blobClient = storageAccount.CreateCloudBlobClient(); var container = blobClient.GetContainerReference(containerName); var blobs = container.ListBlobs(); foreach (var blob in blobs) { Console.WriteLine(blob.GetType().ToString()); } 
+6
source share
1 answer

According to MSDN for CloudBloblContainer.ListBlobs() :

The types of objects returned by the ListBlobs method depend on the type of listing that is being performed. If the UseFlatBlobListing property is set to true, the listing will return an enumerable collection of CloudBlob objects. If UseFlatBlobListing is set to false (the default value), the listing may return a collection containing CloudBlob objects and CloudBlobDirectory objects. The latter case provides a convenience for subsequent enumerations over a virtual blob hierarchy.

So, if you want only blobs, you should set the UseFlatBlobListing property UseFlatBlobListing to true.

 var storageAccount = CloudStorageAccount.Parse(conn); var blobClient = storageAccount.CreateCloudBlobClient(); var container = blobClient.GetContainerReference(containerName); // ** new code below ** // BlobRequestOptions options = new BlobRequestOptions(); options.UseFlatBlobListing = true; // ** new code above ** // var blobs = container.ListBlobs(options); // <-- add the parameter to overload foreach (var blob in blobs) { Console.WriteLine(blob.GetType().ToString()); } 
+9
source

Source: https://habr.com/ru/post/975480/


All Articles