Intro

In the previous post I showed how to work with Azure File Service from powershell, in this post I am going to show how to work with Azure File Service from code.

Prerequisites

Refer to previous posts to get started with Azure File Service.

Code

Azure File Service if very similar to all other Storage Services. In fact, it shares the same method names and signatures. Azure File Storage uses the Azure Table Storage infrastructure to store file and directory metadata that conventional file systems manage in SMB 3.0 clustered solutions. Similarly, Azure File Storage stores each file’s data in an Azure Page Blob. That's why some of the methods of Azure File and Azure Page Blob are almost the same. But let's start with a simple example:

var fileClient = storageAccount.CreateCloudFileClient();
for (var i = 0; i < 1000; i++)
{
    var share = fileClient.GetShareReference($"share{i}");
    share.CreateIfNotExists();
}

This code creates 1000 shares, if they don't exist yet. This is very fast operation, it takes about 5 seconds.

You can use ListShares method to get list of all shares:

var shares = fileClient.ListShares();
Console.WriteLine(shares.Count());

Consider another example:

var share = fileClient.GetShareReference($"share10");
share.Create();

This code throws the following exception:

The remote server returned an error: (409) Conflict.

because the share already exists.

When you create a share from code, you can specify quota. The following code creates a share and sets a quota:

var fileClient = storageAccount.CreateCloudFileClient();
var share = fileClient.GetShareReference($"11jsadfj123");
share.CreateIfNotExists();
share.Properties.Quota = 10;
share.SetProperties();

To delete a share run the following code:

var share = fileClient.GetShareReference($"11jsadfj123");
share.Delete();

Summary

In this post I have showed how to work with Azure File Shares from code. In the next post I am going to continue with Azure File Service, I will show how to work with Azure File Service directories.


;