How to use Fileshare on azure storage

Fileshare component is used for storing any type of files, can be log file, document, pdf or any format, yes, you also can store document and pdf file in blob storage, but in this post we learn how to work with Fileshare storage, specially uploading and downloading file from azure storage.

we will be learning ShareClient object ShareClient share = new ShareClient(_azureStorageConnection, _shareName);, ShareDirectoryClient and ShareFileClient class.

This demo is written using asp.net core 3.1 framework with razor page application.

First, let’s create a form in razor page to upload file, make sure you have added enctype="multipart/form-data" in form tag.

<form method="post" enctype="multipart/form-data">
<input type="file" asp-for="UploadFile" />
<div style="padding:15px;">
	<button asp-page-handler="UploadToFileStorage">Upload to File</button>
</div>
</form>

Notice in handler method we have specified a method name “UploadToFileStorage”, so in code behind there has to be a post method with same name, that’s where the upload code will be written.

Upload file to FileShare storage Azure

Create a IFormFile property and IConfiguration setting.

using Azure.Storage.Files.Shares;
using Azure;

public class filemgr1Model : PageModel
{
    IWebHostEnvironment _webHost;  
    private readonly IConfiguration _configuration;
         
    public filemgr1Model(IWebHostEnvironment webHost, IConfiguration configuration)
    {
            _webHost= webHost;
        _configuration = configuration;
    }

    [BindProperty]
    public IFormFile UploadFile { get; set; }
}

Now write the method, to upload file to azure storage, file will be stored in binary format, so we need to convert the file into stream.

In example below I have kept the share name and directory name hardcoded, you can change that to dynamic value, either from configuration file or database.

public async Task<IActionResult> OnPostUploadToFileStorage()
{
string _fileName = UploadFile.FileName;
string _fileContentType = UploadFile.ContentType;
string _azureStorageConnection = _configuration.GetSection("AzureStorage")["ConnectionString"];
	
	string _actionMessage = "";
	try
	{            
		// share name 
		string _shareName = "sharename1";
		// pass directory name from UI
		string _dirName = "dirctory1";
		// Get a reference to a share and then create it (if not exist)
		ShareClient share = new ShareClient(_azureStorageConnection, _shareName);
		await share.CreateIfNotExistsAsync();
		
		ShareDirectoryClient directory = share.GetDirectoryClient(_dirName);
		await directory.CreateIfNotExistsAsync();
		ShareFileClient file = directory.GetFileClient(_fileName);                
		using (Stream stream = UploadFile.OpenReadStream())
		{
			file.Create(stream.Length);
			await  file.UploadRangeAsync(new HttpRange(0, stream.Length), stream);
		}
		  
		_actionMessage = "Uploaded Successfully to File Storage";
	}
	catch (RequestFailedException ex)
	{
		_actionMessage = ex.ToString();
	}
	return Redirect("filemgr1"); 
}

Now our code is ready, just post some file from web browser and check in your Azure storage account, if file is uploaded to the specified folder.

fileshare azure storage

In your azure portal, go to file share client, then open the folder, you will see the file you uploaded, is there.

You may be interested to learn how to download file from fileshare azure storage in your local machine.

 
Azure Cloud Tutorial
learn azure cloud development

Let's learn how to use Azure Cloud Storage system from Ap.net core c# application.

Data Analysis
learn Artificial Intelligence
Fileshare in Azure storage
Learn Azure Cloud Development