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());
}
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.
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.
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With