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”,
Select “Create a resource”
Type “Storage Account”
Click “create”
Here, you select the subscription and resource group (you can create a new one if required)
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,
Click the create button and the resource group and storage account will be created.
Next, we need to create the file share. Go to the main page,
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,
Next, select “+ File share” to add a new file share as below,
Name the file share and create it.
Then, create a directory under the file share as below,
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.
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,
Once done we will add a class file “AzureFileClient” to the project.
Next, we will add the required Nugget packages,
Now that we have the environment all setup, add the code below to the “Program.cs” and “AzureFileClient.cs” files,
Program.cs
- using AzureFileStorageClient;
- using System;
-
- AzureFileClient.UploadFile();
- Console.WriteLine("The file has been uploaded");
- Console.ReadKey();
AzureFileClient.cs
- using Azure;
- using Azure.Storage.Files.Shares;
- using System.IO;
-
- namespace AzureFileStorageClient
- {
- public class AzureFileClient
- {
- public static void UploadFile()
- {
- var connectionString = "<storage account connection string here>";
- var fileShareName = "munibtestfileshare";
- var folderName = "directory1";
- var fileName = "Testfile.txt";
- var localFilePath = @"c:\temp\Testfile.txt";
-
- ShareClient share = new(connectionString, fileShareName);
-
- var directory = share.GetDirectoryClient(folderName);
-
- var file = directory.GetFileClient(fileName);
- using FileStream stream = File.OpenRead(localFilePath);
- file.Create(stream.Length);
- file.UploadRange(
- new HttpRange(0, stream.Length),
- stream);
- }
- }
- }
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.
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!