working with azure file storage using visual studio 2017 and c

In this blog we shall learn how to work with Azure File Storage using Azure Storage Client Library for .NET and Visual Studio 2017 with C# as programming language.

For novice Cloud developers, aspiring Cloud architects and all other IT professionals associated with Cloud computing, especially with Microsoft Azure, this blog will explain the importance and capabilities of Microsoft Azure File Storage.

Pre-requisite

Quick Overview
Azure File storage service,

Getting Started
For brevity of this blog, we have created Microsoft Azure Storage account using Microsoft Azure portal.
Click here, to learn how to create Azure Storage account using Microsoft Azure portal. (Will share article soon)
Navigate to Storage Account => Overview section.

img

Navigate to Access keys section.
Copy Storage account name and key (any one).
We shall require this details in order to get connected with Azure storage account in our application.

img

Open Visual Studio, we will be using Visual Studio 2017 v15.5
Create a new ASP.NET console application.
Give any name to application, here we shall name it as ‘LeaningAzureFiles’.
Go to Solution Explorer => Right click on Project => Click, Manage NuGet Packages.
Enter ‘Windows Azure Storage SDK’ under Browse tab => Search box
With Include prerelease box as checked.

Select the very first listed library, as shown in below image.
WindowsAzure.Storage: Client library for working with Microsoft Azure storage services including blobs, files, table and queues.

This library is provided by Microsoft with current stable version as 8.4.0 (at the time of writing this blog)
Click on Install

.
img

Once library is installed, we can notice an addition of new libraries in our project reference folder.
Open file Program.cs
Add following code to import required namespaces,

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.File;

Declare two Constant string variables,


Using above values or details, we shall create connection with our Azure Storage from application.
For doing so, create a new instance of CloudStorageAccount,

CloudStorageAccount represents Microsoft Azure Storage Account. This class is from Microsoft.WindowsAzure.Storage namespace. It initializes Azure storage account instance.

It requires two parameters,

For clearer picture on steps mentioned above, Refer below image,


img

Build the application and Run.
Place a debugger in code.
As highlighted in below image,
Connection with Azure Storage account is created and endpoints details for Storage Files can be seen.

img

Create Azure File Share
For creating Azure File share,
We need to create an Azure File service client using CloudStorageAccount instance.


Refer below image, for above mentioned implementation.


img

Run the application.
Now, open Microsoft Azure portal,
Navigate to Storage Account => Overview section.
Click on Files, under Services block, as shown in below image.
We can see newly created Azure File share naming ‘learning-file-share’ , along with its URL or endpoint to get connected.


Creating File in Root Directory
For creating file in Root directory in to our Azure file, we shall use


Below image, shows UploadText method in detail,


img

Refer below image, for above mentioned implementation


img

Run the application.
Now, open Microsoft Azure portal,
Navigate to Storage Account => Overview section.
Click on Files => learning-file-share (File share created)
Great! new file is now created.

img

As we have used UploadText method to upload text string in newly created file, let open up the file to verify it once.
In portal, Click on File i.e. ‘my-first-file.txt’
Blade with name File properties will appear at right corner of portal screen.
Details like, File name, size, public URL, last modified date, ETAG along with two links,

Click on Download


img

Once the file gets downloaded, open the file.
We can see the string text we passed in UploadText method, is part of our file.


img

Creating Folders & Files in Root Directory
For creating folders & file in Root directory in to our Azure File, we shall use,

Refer below image, for above mentioned implementation.


img

Run the application.
Now, open Microsoft Azure portal,
Navigate to Storage Account => Overview section.
Click on Files => learning-file-share (File share created).
We can see newly created folder at File share root.


img

Now, create file with some text under this given folder.
Repeat above mentioned steps for creating a file.
The only change here would be, now its not on root directory but in folder.
Refer below image for detailed implementation.

img

Run the application.
Verify in Azure portal.


img

In similar fashion we can create multiple files, folders in Azure File share.
Let’s repeat the same activity for creating folder & file in folder.
Code Implementation,


img

Run application & verify in Azure Portal.
Newly created folder.


img

Newly created file inside Folder.


img

Mounting Azure File Share
Azure storage allows mounting of Azure File Share in to our Windows & Linux machine.
Connecting from Windows
When connecting from a computer from outside Azure, remember to open outbound TCP port 445 in your local network. Some Internet service providers may block port 445. Check with your service provider for details.
It gets connect with running anyone from PowerShell commands & commands.
These commands are already provided in Azure portal. Just copy & Run!
Learn more about Azure File Storage with Windows
Connecting from Linux
The Linux SMB3 client doesn't support share level encryption yet, so mounting a file share in Linux only works from virtual machines running in the same Azure region as the file share.
Learn more about Azure File Storage with Linux
Refer below image for the commands.


img

As an exercise, will recommend to try this of your own with reading the links provided. This will help you in understanding the concept in much better manner.
Downloading File Share contents via programming
Write a method for downloading all folders & files from File Share. This method accepts File Share Root directory object & Location of machine where we need to download this file.

Using above entities, method will check files & folders from given Fil Share root directory and accordingly download the files at provided location.

Below image shows the above implementation with step by steps details as comments.



img

Calling above Method with passing required parameters as mentioned above.


img

Compile & Run the application.
All the above created files & folders are downloaded at provided location.


img

Open the file to verify the content.
Great folders, files & content are verified!


img

In this blog we learned how to get connect with Azure Storage account, create new Azure Storage File share along with adding folders & files in to Azure file share. Also, how to mount & download file share contents all in ASP.NET application using C# as programming language.

Hope this blog will help developers to easily get started with Azure File share using C#. Would recommend to try working with Azure Files, to leverage azure storage power in to your application.

Chirag Tumar, A 5-years experienced developer in Nexsoftsys, has vast knowledge about content writing. Nexsoftsys is an Offshore ASP.NET development Company India. Chirag wrote many technical content from his experience. Thank You!