Using Azure File Storage In C#

Introduction

 
In today’s article, we will look at a feature of Microsoft Azure, which is Microsoft’s Cloud offering. One of the main services provided with Azure is Cloud storage. This allows us to store different artifacts in the cloud. These include Tables, Queues, Files, and Containers. In this article, we will look at how to create an Azure file share and upload a file to this share using C#.

First, create the Azure Storage Account and Container

 
In order to complete this step, you would need a Microsoft Azure account with an active subscription. You can get a free one for initial use. However, as my free subscription ended a long time ago, I have setup a pay-as-you-go one. 

Once you have created your Azure portal account and subscription, you see the below. You can see I have one subscription which is “Pay-As-You-Go”,
 
Using Azure File Storage In C# 
 
Select “Create a resource”
 
Using Azure File Storage In C# 
 
Type “Storage Account”
 
Using Azure File Storage In C#
 
Using Azure File Storage In C# 
 
Click “create”
 
Using Azure File Storage In C#
 
Here, you select the subscription and resource group (you can create a new one if required)
 
Using Azure File Storage In C# 
 
Then, enter a storage account name, region, performance, and redundancy value. I will not discuss these options in detail here as there are many articles that detail them.
 
Once done click “Review + Create”, you will see the below,
 
Using Azure File Storage In C# 
 
Click the create button and the resource group and storage account will be created.
 
Using Azure File Storage In C#
 
Using Azure File Storage In C# 
 
Next, we need to create the file share. Go to the main page,
 
Using Azure File Storage In C# 
 
Here, you see the resource group and a storage account you have just created. Select the storage account and then the “File shares” option under “Data storage” as below,
 
Using Azure File Storage In C# 

Next, select “+ File share” to add a new file share as below,
 
Using Azure File Storage In C# 

Name the file share and create it.
 
Then, create a directory under the file share as below,
 
Using Azure File Storage In C#
 
Using Azure File Storage In C#
 
Using Azure File Storage In C#
 
Using Azure File Storage In C# 
 
Here you can see that the directory is empty.

Finally, copy the connection string from the storage account as this is needed to access the file share from the C# code.
 
Using Azure File Storage In C# 
 
Now, we will create our C# application to upload a file to this file share we have just created.

Next, create the Visual Studio 2019 application


Next, we will create a console application using Visual Studio 2019 Community edition as below,
 
Using Azure File Storage In C#
 
Using Azure File Storage In C#
 
Using Azure File Storage In C#
 
Using Azure File Storage In C# 
 
Once done we will add a class file “AzureFileClient” to the project.
 
Using Azure File Storage In C# 
 
Next, we will add the required Nugget packages,
 
Using Azure File Storage In C#
 
Using Azure File Storage In C#
 
Using Azure File Storage In C#
 
Using Azure File Storage In C# 
 
Now that we have the environment all setup, add the code below to the “Program.cs” and “AzureFileClient.cs” files,
 
Program.cs
  1. using AzureFileStorageClient;  
  2. using System;  
  3.   
  4. AzureFileClient.UploadFile();  
  5. Console.WriteLine("The file has been uploaded");  
  6. Console.ReadKey();  
AzureFileClient.cs
  1. using Azure;  
  2. using Azure.Storage.Files.Shares;  
  3. using System.IO;  
  4.   
  5. namespace AzureFileStorageClient  
  6. {  
  7.     public class AzureFileClient  
  8.     {  
  9.         public static void UploadFile()  
  10.         {  
  11.             var connectionString = "<storage account connection string here>";  
  12.             var fileShareName = "munibtestfileshare";  
  13.             var folderName = "directory1";  
  14.             var fileName = "Testfile.txt";  
  15.             var localFilePath = @"c:\temp\Testfile.txt";  
  16.   
  17.             ShareClient share = new(connectionString, fileShareName);  
  18.   
  19.             var directory = share.GetDirectoryClient(folderName);  
  20.   
  21.             var file = directory.GetFileClient(fileName);  
  22.             using FileStream stream = File.OpenRead(localFilePath);  
  23.             file.Create(stream.Length);  
  24.             file.UploadRange(  
  25.                 new HttpRange(0, stream.Length),  
  26.                 stream);  
  27.         }  
  28.     }  
  29. }  
Now, we run the application and after that when we look into our file share and directory, we see that the new file has been uploaded there.
 
Using Azure File Storage In C#
 
Using Azure File Storage In C# 

Summary

 
In this article, we looked at creating a Microsoft Azure storage account and then added a file share to it. Then, we created a Visual Studio project to upload a file to a directory under this file share. In the coming articles, I will talk about the other data storage types. Happy coding!