question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Storage] How to get all directories in the container in the latest v12 .NET SDK?

See original GitHub issue

How to get all directories in the container in the latest v12 storage SDK?

I have the following structure in my blob storage:

  • container1
    • directory1 - file1
    • directory2 - file1 - file2
  • container2

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mzrkscommented, Jun 3, 2020

@amnguye thanks for the example! This is what I was looking for!

1reaction
amnguyecommented, May 28, 2020

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();
            }
        }
Read more comments on GitHub >

github_iconTop Results From Across the Web

List blobs with .NET - Azure Storage
When you call a listing operation hierarchically, Azure Storage returns the virtual directories and blobs at the first level of the hierarchy.
Read more >
Is there a way to get file structure from azure blob?
I'm new to Azure and playing around with blobs in my .Net application. I want to be able to get structure with folders,...
Read more >
Azure Blob Storage using a .NET Core Console Application
Create a Container. You can choise the Container name. First create a new instance of BlobServiceClient. Then call the CreateBlobContainerAsync method to create ......
Read more >
Azure.Storage.Blobs 12.17.0
This client library enables working with the Microsoft Azure Storage Blob service for storing binary and text data. For this release see notes...
Read more >
Get a list of blobs in a container with the v12 SDK for .NET-C
After some further research I came across the Factory Pattern, and using that I am now seeing List<Item> Items correctly populated after every...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found