How to get all directories in the container in the latest v12 storage SDK?
I have the following structure in my blob storage:
I`m interested to get all directories(without files) that exist in the container1. If something were found I would like to process this directory deeper. How can I implement it?
In the v11 version we had lot of classes/methods that don`t exist in the v12. It would be good to have some manual on how to migrate to v12.
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @xgithubtriage.
Hello,
Thanks for your question!
Was there a convenience method(s) or class(es) you were referring to from the v11 library that you were looking for in the v12 library? If so what is the name of the convenience method(s) or class(es)?
To only list the directories in a blob container here's a snippet of code I think that you could take some inspiration from.
The REST API does not allow us an option to look for just top level directories, we have to get the list of blobs and parse for directories.
https://docs.microsoft.com/en-us/rest/api/storageservices/list-blobs
**Updated to use GetBlobsByHierarchyAsync
/// <summary>
/// List all the directories in a container.
/// </summary>
[Test]
public async Task ListDirectoriesAsync()
{
// Get a connection string to our Azure Storage account.
string connectionString = ConnectionString;
// Get a reference to a container named "sample-container" and then create it
BlobContainerClient container = new BlobContainerClient(connectionString, Randomize("sample-container"));
await container.CreateAsync();
try
{
// Upload a couple of blobs so we have something to list
await container.UploadBlobAsync("first/file1", File.OpenRead(CreateTempFile()));
await container.UploadBlobAsync("first/file2", File.OpenRead(CreateTempFile()));
await container.UploadBlobAsync("second/fileA", File.OpenRead(CreateTempFile()));
await container.UploadBlobAsync("third/fileB", File.OpenRead(CreateTempFile()));
await container.UploadBlobAsync("first/fourth/file3", File.OpenRead(CreateTempFile()));
await container.UploadBlobAsync("first/fourth/file4", File.OpenRead(CreateTempFile()));
// List all the directories
Queue<string> prefixes = new Queue<string>();
prefixes.Enqueue("");
List<string> directoryNames = new List<string>();
do
{
string prefix = prefixes.Dequeue();
await foreach (BlobHierarchyItem blobHierarchyItem in container.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/"))
{
if (blobHierarchyItem.IsPrefix)
{
directoryNames.Add(blobHierarchyItem.Prefix);
prefixes.Enqueue(blobHierarchyItem.Prefix);
}
}
} while (prefixes.Count > 0);
Assert.AreEqual(4, directoryNames.Count);
Assert.Contains("first/", directoryNames);
Assert.Contains("second/", directoryNames);
Assert.Contains("third/", directoryNames);
Assert.Contains("first/fourth/", directoryNames);
}
finally
{
// Clean up after the test when we're finished
await container.DeleteAsync();
}
}
@amnguye , you might be able to filter directories by using deliminator="/" in the container.GetBlobsAsync() call. To get the next level, we could use both prefix and delimitor.
Thanks @seanmcc-msft ! I just updated the sample I posted yesterday to use a delimiter, and also to recursively keep looking for subdirectories as needed.
@amnguye thanks for the example! This is what I was looking for!