private static async Task ListBlobsHierarchicalListingAsync(CloudBlobContainer container, string prefix)
{
// List blobs in segments.
Console.WriteLine("List blobs (hierarchical listing):");
Console.WriteLine();
// Enumerate the result segment returned.
BlobContinuationToken continuationToken = null;
BlobResultSegment resultSegment = null;
try
{
// Call ListBlobsSegmentedAsync recursively and enumerate the result segment returned, while the continuation token is non-null.
// When the continuation token is null, the last segment has been returned and execution can exit the loop.
// Note that blob snapshots cannot be listed in a hierarchical listing operation.
do
{
resultSegment = await container.ListBlobsSegmentedAsync(prefix, false, BlobListingDetails.Metadata, null, null, null, null);
foreach (var blobItem in resultSegment.Results)
{
Console.WriteLine("************************************");
Console.WriteLine(blobItem.Uri);
// A hierarchical listing returns both virtual directories and blobs.
// Call recursively with the virtual directory prefix to enumerate the contents of each virtual directory.
if (blobItem is CloudBlobDirectory)
{
PrintVirtualDirectoryProperties((CloudBlobDirectory)blobItem);
CloudBlobDirectory dir = blobItem as CloudBlobDirectory;
await ListBlobsHierarchicalListingAsync(container, dir.Prefix);
}
else
{
// Write out blob properties and metadata.
PrintBlobPropertiesAndMetadata((CloudBlob)blobItem);
}
}
Console.WriteLine();
// Get the continuation token, if there are additional segments of results.
continuationToken = resultSegment.ContinuationToken;
} while (continuationToken != null);
}
catch (StorageException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}