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:
- 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.
- 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.
- We will combine fashionable purposes with the assistance of Azure File Storage.
- 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.
Perquisites Required
- Microsoft Visual Studio 2017
- 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.
Step 2
Now in File Share, click on on the File Share button to create an Azure File Share Endpoint.
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.
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.
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.
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.
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
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.
- <?xml model=“1.0” encoding=“utf-8” ?>
- <configuration>
- <appSettings>
- <add key=“fileShareConnectionString”worth=“DefaultEndpointsProtocol=https;AccountName=accountName;AccountKey=keyName” />
- <add key=“fileShareConnectionString” worth=“DefaultEndpointsProtocol=https;AccountName=cloudshell494327991;AccountKey=v87L6Zor0sEHjL0CeU7i8ujiVW+eU+9vorCQIP/Q0mCEJnihQYOJVfIiRDxYe1450+ksn+R2HbhByYPhcB2Etg==” />
- </appSettings>
- </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.
- static void Fundamental(string[] args)
- {
- CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(“fileShareConnectionString”));
- CloudFileClient cloudFileClient = storageAccount.CreateCloudFileClient();
- CloudFileShare fileShare = cloudFileClient.GetShareReference(“filesharedemo”);
- if (fileShare.Exists())
- {
- CloudFileDirectory fileDirectory = fileShare.GetRootDirectoryReference();
- CloudFileDirectory customDirectory = fileDirectory.GetDirectoryReference(“CustomDocs”);
- if (customDirectory.Exists())
- {
- CloudFile fileInfo = customDirectory.GetFileReference(“Log1.txt”);
- if (fileInfo.Exists())
- {
- Console.WriteLine(fileInfo.DownloadTextAsync().Outcome);
- }
- }
- NewFileCreate();
- }
- }
Step 10
Now, Execute this system to examine the output.
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.
- public static void NewFileCreate()
- {
- CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
- CloudFileShare share = fileClient.GetShareReference(“filesharedemo”);
- if (share.Exists())
- {
- string policyName = “sampleSharePolicy” + DateTime.UtcNow.Ticks;
- SharedAccessFilePolicy sharedPolicy = new SharedAccessFilePolicy()
- SharedAccessFilePermissions.Write
- ;
- FileSharePermissions permissions = share.GetPermissions();
- permissions.SharedAccessPolicies.Add(policyName, sharedPolicy);
- share.SetPermissions(permissions);
- CloudFileDirectory rootDir = share.GetRootDirectoryReference();
- CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(“CustomDocs”);
- CloudFile file = sampleDir.GetFileReference(“Log1.txt”);
- string sasToken = file.GetSharedAccessSignature(null, policyName);
- Uri fileSasUri = new Uri(file.StorageUri.PrimaryUri.ToString() + sasToken);
- CloudFile fileSas = new CloudFile(fileSasUri);
- fileSas.UploadText(“This file created by the Console App at Runtime”);
- Console.WriteLine(fileSas.DownloadText());
- }
- }
Step 12
Now execute this system and examine the File Share explorer in Azure Portal.
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.