TIP

💡 Learn more : Azure storage account overview.

Creating an Azure Blob Hierarchy

We've reviewed the following options with Azure Storage so far:

Today, we are going to look at creating an Azure blob hierarchy via C#. Go ahead and open the Azure Portal and open the C# app that we worked with earlier. If you want to start from this post, then use the code located here.

The goal of this exercise is to create a blob hierarchy or folder structure inside of our container. So for example, we'd like to place a file in a structure such as backup/images-backup.png.

If you look below, you will notice that there is no way to create a folder structure from inside the portal.

But we can easily do this with code by adding the folder structure we want into the code as shown below.

static void Main(string[] args)
{
   BlobServiceClient storageAccount = new BlobServiceClient(CloudConfigurationManager.GetSetting("StorageConnection"));
   BlobContainerClient container = storageAccount.GetBlobContainerClient("images-backup");
   container.CreateIfNotExists(PublicAccessType.Blob);

   // line modified
   BlockBlobClient blockBlob = container.GetBlockBlobClient("backup/mikepic.png");
   using (var fileStream = System.IO.File.OpenRead(@"c:\mikepic.png"))
   {
         blockBlob.Upload(fileStream);
   }
   // line modified

   Console.ReadLine();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

If we run the application and switch over to our Storage Account and navigate inside the container, we'll see our container now has a folder: