Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

container.ListBlobs is giving a list of CloudBlobDirectory I was expecting a list of CloudBlockBlobs?

I am using container.ListBlobs, but it seems to be returning a list {Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.LazyEnumerable} however when I do a foreach the object seems to be CloudBlobDirectory instead of a list of CloudBlockBlobs. Am I doing something wrong, or is this what it should return? Is there some way I can just get a list of the blobs, rather than 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());
}
like image 920
DermFrench Avatar asked Sep 18 '14 10:09

DermFrench


People also ask

What is ListBlobs?

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.

How do I upload an image to Azure?

Sign in to the Azure portal. From the left menu, select Storage accounts, then select the name of your storage account. Select Containers, then select the thumbnails container. Select Upload to open the Upload blob pane.


1 Answers

According to the 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 only want blobs, you have to set the UseFlatBlobListing property option 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());
}
like image 117
Bob. Avatar answered Oct 19 '22 23:10

Bob.