Azure

Implement Azure File Storage Utilizing ASP.NET Core Console Software

Introduction 

 

At this time, on this article we’ll talk about the right way to develop an internet utility or console-based utility to retailer information into the Azure File Storage. Everyone knows that File Storage is part of the Azure Storage. So first, we’ll talk about some primary ideas about azure file storage after which we’ll talk about the right way to retailer information into the azure file storage.

 

Microsoft supplies 4 several types of storage throughout the Azure Storage aside from Azure SQL and Azure Cosmos DB. Azure Storage accounts present 4 several types of information companies by which we will save information into the Azure storage accounts. The under picture exhibits all the weather of Azure Storage.

 

 

Idea of Azure File Storage

 

Microsoft introduced the Azure File Storage idea within the yr of 2015. With the assistance of Azure File Storage, Microsoft Azure can present us absolutely managed file shares within the cloud setting. Since Azure File Storage uncovered file shares mechanism utilizing the Server Message Block 3.0 (SMB) protocol. This protocol predominantly used within the file share course of for any present on-premises purposes. So, on this means, it is rather straightforward to maneuver our utility from the on-premises setting to the cloud environments. Azure File Storage can even implement by REST API protocol, which supplies us means to develop any fashionable utility which must combine with present purposes.

 

Azure File Storage is designed to assist several types of enterprise situation:

  1. Migrating an present utility from the on-premises setting to the cloud. Azure File Storage supplies us an choice to migrate our on-premises file or file share-based purposes to the Cloud setting with out managing any highly-available file server VMs.
  2. Prospects can share server information throughout on-premises and cloud servers each. Many purposes now retailer information resembling log information, occasion information, and backups within the cloud setting to attain higher availability, sturdiness, scalability, and geo-redundancy. So, with the encryption in SMB 3.0, we will securely mount Azure File Storage shared from anyplace.
  3. We will combine fashionable purposes with the assistance of Azure File Storage.
  4. We will simplify the internet hosting excessive availability (HA) workload information with the assistance of Azure File Storage. Since Azure File Storage supplies steady availability, so it simplifies the trouble to host HA workload information within the cloud.

Advantages of Azure File Storage

 

So, the principle advantages of the Azure File Storage are:

  • Azure File Storage is rather more safe as a result of our information is encrypted at relaxation and through transit, both SMB 3.Zero or HTTPS protocol is used for the information.
  • Azure File Storage is way smarter since we will entry our information over excessive latency. Additionally, we will entry the information in low bandwidth hyperlinks utilizing sensible caching of generally used on-premises information utilizing Azure File Sync.
  • Azure File Methods helps Cross-Platform. We will mount our Azure File Share from Home windows, Linux, or macOS primarily based server additionally.
  • Azure File Methods will be simply managed because the deployment of Azure File Server doesn’t require any {hardware} or working system associated to deployment administration. So, we will instantly deal with our customers as an alternative of {hardware} or working system administration.
  • We will implement Azure File Server in a Hydrid mannequin like we will entry our information from anyplace we would like with the assistance of SMB, REST, and even on-premises with Azure File Sync.
  • We will instantly migrate our file share-dependent utility to the cloud with out breaking or altering the prevailing code.
Implement Azure File Storage Using ASP.NET Core Console Application

Perquisites Required

  1. Microsoft Visual Studio 2017
  2. The account in Azure Portal.

In the event you don’t have any present Azure account, then you possibly can create a free trial account within the Azure portal utilizing your e-mail ID.

 

Add Recordsdata in Azure File Storage Account utilizing Azure Portal or Powershell

 

For utilizing Azure File Storage, we first must create a Storage Accounts in Azure Portal. As soon as the Storage Account has been created, click on on the account to open in Storage Account Explorer.

 

Step 1

 

Now click on on the File Share possibility within the Storage Explorer.

 

Implement Azure File Storage Using ASP.NET Core Console Application

 

Step 2

 

Now in File Share, click on on the File Share button to create an Azure File Share Endpoint.

 

Implement Azure File Storage Using ASP.NET Core Console Application

 

Step 3

 

Present the File Share account title and likewise we will present the Quota (i.e. the utmost measurement allocation of the File Share). Initially we offer 2 GB. Later we will improve or lower the quota quantity.

 

Implement Azure File Storage Using ASP.NET Core Console Application

 

Step 4

 

Now within the File Share listing, click on on the File Share which we simply created. It should open the File Share Entry level. In that window, click on on the Join button to retrieve the connection string. With the assistance of this connection command, we will mount the Azure File Share account as a drive from our home windows or every other OS primarily based computer systems.

 

Implement Azure File Storage Using ASP.NET Core Console Application

 

Step 5

 

Now copy the above script and execute it within the Powershell window. After working the script, Azure File Storage is mounted within the laptop as Z drive.

 

Implement Azure File Storage Using ASP.NET Core Console Application

 

Step 6

 

Now, we will create any information as we would like and it is going to be mechanically uploaded within the Azure File Storage account. Each file uploaded within the Azure File Storage account will present a public URL by which we will entry that file.

 

Implement Azure File Storage Using ASP.NET Core Console Application

 

Add Recordsdata in Azure File Share utilizing .NET Core Console Software

 

Step 1

 

Now open Microsoft Visual Studio 2017 and click on on File –> New –> Tasks.

 

Step 2

 

Choose Console App Undertaking template and Click on on the Okay Button

 

Step 3 

 

Earlier than we begin coding, we first want to put in some packages which assist us to implement Azure File Storage within the console purposes.

 

Step 4 

 

So, within the Resolution Explorer, right-click to our mission and select Handle Nuget Packages.

 

Step 5 

 

Now, within the Nuget Bundle Supervisor, choose Browse.

 

Step 6 

 

Now, seek for the Bundle Microsoft.Azure.Storage.Blob after which Set up.

 

Step 7 

 

Equally, set up the under packages:

  • Microsoft.Azure.Storage.Widespread
  • Microsoft.Azure.Storage.File
  • Microsoft.Azure.ConfigurationManager
Implement Azure File Storage Using ASP.NET Core Console Application

 

Step 8

 

Now Open the App.config file and add the connection string associated to the Azure File Share. Within the Connection String, substitute the accountName with Azure Storage Account title and keyName with the Azure Storage Account key.

  1. <?xml model=“1.0” encoding=“utf-8” ?>  
  2. <configuration>  
  3.   <appSettings>  
  4.     <add key=“fileShareConnectionString”worth=“DefaultEndpointsProtocol=https;AccountName=accountName;AccountKey=keyName” />  
  5.     <add key=“fileShareConnectionString” worth=“DefaultEndpointsProtocol=https;AccountName=cloudshell494327991;AccountKey=v87L6Zor0sEHjL0CeU7i8ujiVW+eU+9vorCQIP/Q0mCEJnihQYOJVfIiRDxYe1450+ksn+R2HbhByYPhcB2Etg==” />  
  6.   </appSettings>  
  7. </configuration>  

Step 9

 

Write the under code to entry the File Storage account in Azure. With the assistance of this code, we will entry the Log.txt file from the Azure File Share account and show the content material of the file into the console.

  1. static void Fundamental(string[] args)  
  2.         {    
  3.               
  4.             CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(“fileShareConnectionString”));  
  5.               
  6.             CloudFileClient cloudFileClient = storageAccount.CreateCloudFileClient();  
  7.   
  8.               
  9.             CloudFileShare fileShare = cloudFileClient.GetShareReference(“filesharedemo”);  
  10.   
  11.               
  12.             if (fileShare.Exists())  
  13.             {  
  14.                   
  15.                 CloudFileDirectory fileDirectory = fileShare.GetRootDirectoryReference();  
  16.   
  17.                   
  18.                 CloudFileDirectory customDirectory = fileDirectory.GetDirectoryReference(“CustomDocs”);  
  19.   
  20.                   
  21.                 if (customDirectory.Exists())  
  22.                 {  
  23.                       
  24.                     CloudFile fileInfo = customDirectory.GetFileReference(“Log1.txt”);  
  25.   
  26.                       
  27.                     if (fileInfo.Exists())  
  28.                     {  
  29.                           
  30.                         Console.WriteLine(fileInfo.DownloadTextAsync().Outcome);  
  31.                     }  
  32.                 }  
  33.   
  34.                 NewFileCreate();  
  35.             }  
  36.         }  

Step 10 

 

Now, Execute this system to examine the output.

 

Implement Azure File Storage Using ASP.NET Core Console Application

Step 11 

 

Within the subsequent half, we’ll attempt to create a brand new file programmatically. To create a brand new file, we have to outline or generate a Shared Entry Signature (SAS) for a file share or a person file. we will additionally create the entry coverage for a file share to handle the shared entry signature. Within the under part, we create a saved entry coverage on the file share. This instance makes use of that coverage to supply the constraints for a SAS on a file within the share.

  1. public static void NewFileCreate()  
  2.         {  
  3.               
  4.             
  5.             CloudFileClient fileClient = storageAccount.CreateCloudFileClient();  
  6.             CloudFileShare share = fileClient.GetShareReference(“filesharedemo”);  
  7.               
  8.             if (share.Exists())  
  9.             {  
  10.                 string policyName = “sampleSharePolicy” + DateTime.UtcNow.Ticks;  
  11.                  
  12.                 SharedAccessFilePolicy sharedPolicy = new SharedAccessFilePolicy()  
  13.                  SharedAccessFilePermissions.Write  
  14.                 ;  
  15.   
  16.                 FileSharePermissions permissions = share.GetPermissions();  
  17.                   
  18.                 permissions.SharedAccessPolicies.Add(policyName, sharedPolicy);  
  19.                 share.SetPermissions(permissions);  
  20.                   
  21.                 CloudFileDirectory rootDir = share.GetRootDirectoryReference();  
  22.                 CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(“CustomDocs”);  
  23.   
  24.                 CloudFile file = sampleDir.GetFileReference(“Log1.txt”);  
  25.                 string sasToken = file.GetSharedAccessSignature(null, policyName);  
  26.                 Uri fileSasUri = new Uri(file.StorageUri.PrimaryUri.ToString() + sasToken);  
  27.                   
  28.                   
  29.                 CloudFile fileSas = new CloudFile(fileSasUri);  
  30.                 fileSas.UploadText(“This file created by the Console App at Runtime”);  
  31.                 Console.WriteLine(fileSas.DownloadText());  
  32.             }  
  33.         }  

Step 12 

 

Now execute this system and examine the File Share explorer in Azure Portal.

 

Implement Azure File Storage Using ASP.NET Core Console Application

 

Conclusion

 

On this article, we talk about the right way to retailer information utilizing the ASP.NET Core utility in Azure File Storage. Any options or suggestions associated to this text are most welcome.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button