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());
}
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());
}