Upload File to Azure File Storage C#
Introduction
In this article we will talk over how to develop a web application or console-based application to shop data into the Azure File Storage. We all know that File Storage is a part of Azure Storage. So showtime, we volition discuss some basic concepts about Azure file storage and so nosotros volition discuss how to store data into the Azure file storage.
Microsoft provides four different types of storage inside the Azure Storage except for Azure SQL and Azure Creation DB. Azure Storage accounts provide four different types of information services through which nosotros tin can save data into the Azure storage accounts. The below paradigm shows all the elements of Azure Storage.
Concept of Azure File Storage
Microsoft announced the Azure File Storage concept in the yr of 2015. With the help of Azure File Storage, Microsoft Azure can provide usa fully managed file shares in the cloud surround, since Azure File Storage exposed the file share machinery using the Server Message Cake three.0 (SMB) protocol. This protocol is predominantly used in the file share process for whatever existing on-premises applications. So, in this way, it is very easy to motion our application from the on-premises surround to the cloud environments. Azure File Storage can also implement by REST API protocol, which provides united states of america the means to develop whatever mod awarding which needs to integrate with existing applications.
Azure File Storage is designed to back up unlike types of business scenarios:
- Migrating an existing application from the on-premises environment to the cloud. Azure File Storage provides usa an pick to migrate our on-bounds file or file share-based applications to the Deject environment without managing any highly-available file server VMs.
- Customers can share server data across on-premises and cloud servers. Many applications at present store data such as log files, event data, and backups in the deject environment to reach better availability, immovability, scalability, and geo-back-up. So, with the encryption in SMB 3.0, nosotros can securely mount Azure File Storage shared from anywhere.
- Nosotros can integrate modernistic applications with the help of Azure File Storage.
- We tin simplify the hosting high availability (HA) workload data with the help of Azure File Storage. Since Azure File Storage provides continuous availability, it simplifies the effort to host HA workload data in the cloud.
Benefits of Azure File Storage
So, the main benefits of the Azure File Storage are:
- Azure File Storage is much more secure because our data is encrypted at rest and during transit, either SMB iii.0 or HTTPS protocol is used for the information.
- Azure File Storage is much smarter since nosotros can admission our files over loftier latency. Also, we tin admission the files in low bandwidth links using smart caching of unremarkably used on-premises files using Azure File Sync.
- Azure File Systems supports Cross-Platform. We can mount our Azure File Share from Windows, Linux, or macOS based server besides.
- Azure File Systems can be hands managed since the deployment of Azure File Server does not require any hardware or operating system related to deployment management. So, nosotros can directly focus on our users instead of hardware or operating arrangement direction.
- We can implement Azure File Server in a Hydrid model like we can admission our data from anywhere nosotros want with the help of SMB, Residuum, or fifty-fifty on-premises with Azure File Sync.
- We can directly migrate our file share-dependent awarding to the cloud without breaking or changing the existing code.
Perquisites Required
- Microsoft Visual Studio 2017
- The account in Azure Portal.
If you don't have any existing Azure account, and so you can create a free trial account in the Azure portal using your e-mail ID.
Upload Files in Azure File Storage Account using Azure Portal or Powershell
For using Azure File Storage, we first demand to create a Storage Accounts in Azure Portal. Once the Storage Business relationship has been created, click on the account to open in Storage Account Explorer.
Step 1
At present click on the File Share option in the Storage Explorer.
Step 2
Now in File Share, click on the File Share button to create an Azure File Share Endpoint.
Step three
Provide the File Share account proper noun and also nosotros tin can provide the Quota (i.due east. the maximum size allocation of the File Share). Initially we provide 2 GB. Later nosotros can increment or decrease the quota volume.
Stride 4
Now in the File Share list, click on the File Share which we just created. Information technology will open the File Share Admission bespeak. In that window, click on the Connect button to recollect the connectedness string. With the help of this connection command, we tin mount the Azure File Share account as a bulldoze from our windows or any other Os based computers.
Step 5
Now re-create the above script and execute it in the Powershell window. Afterward running the script, Azure File Storage is mounted in the reckoner as Z drive.
Footstep 6
Now, nosotros can create whatever files every bit we want and it volition be automatically uploaded in the Azure File Storage account. Every file uploaded in the Azure File Storage account volition provide a public URL through which we can access that file.
Upload Files in Azure File Share using .NET Core Console Awarding
Pace 1
Now open Microsoft Visual Studio 2017 and click on File --> New --> Projects.
Stride 2
Select Console App Project template and Click on the Ok Push button
Step 3
Before nosotros kickoff coding, nosotros first need to install some packages which assistance us to implement Azure File Storage in the panel applications.
Step 4
So, in the Solution Explorer, right-click to our project and cull Manage Nuget Packages.
Step 5
Now, in the Nuget Package Managing director, select Browse.
Step 6
At present, search for the Parcel Microsoft.Azure.Storage.Blob and then Install.
Step seven
Similarly, install the below packages:
- Microsoft.Azure.Storage.Common
- Microsoft.Azure.Storage.File
- Microsoft.Azure.ConfigurationManager
Stride 8
Now Open up the App.config file and add the connection cord related to the Azure File Share. In the Connection Cord, replace the accountName with Azure Storage Account name and keyName with the Azure Storage Business relationship key.
- <?xml version= "1.0" encoding= "utf-8" ?>
- <configuration>
- <appSettings>
- <add fundamental="fileShareConnectionString" value= "DefaultEndpointsProtocol=https;AccountName=accountName;AccountKey=keyName" />
- <add together key="fileShareConnectionString" value= "DefaultEndpointsProtocol=https;AccountName=cloudshell494327991;AccountKey=v87L6Zor0sEHjL0CeU7i8ujiVW+eU+9vorCQIP/Q0mCEJnihQYOJVfIiRDxYe1450+ksn+R2HbhByYPhcB2Etg==" />
- </appSettings>
- </configuration>
Pace 9
Write the beneath code to admission the File Storage account in Azure. With the help of this lawmaking, we can access the Log.txt file from the Azure File Share account and display the content of the file into the console.
- static void Main( 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().Result);
- }
- }
- NewFileCreate();
- }
- }
Step x
Now, Execute the program to check the output.
Step 11
In the next part, we volition try to create a new file programmatically. To create a new file, we need to define or generate a Shared Access Signature (SAS) for a file share or an individual file. We can also create the access policy for a file share to manage the shared access signature. In the below section, we create a stored admission policy on the file share. This case uses that policy to provide the constraints for a SAS on a file in 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()
- {
- SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
- Permissions = SharedAccessFilePermissions.Read | 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( cypher , 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());
- }
- }
Footstep 12
Now execute the program and check the File Share explorer in Azure Portal.
Conclusion
In this commodity, nosotros hash out how to store information using the ASP.NET Core application in Azure File Storage. Any suggestions or feedback related to this article are most welcome.
Also, if everyone wants to read the other articles related to the Azure Storage, and then follow the below links,
- Upload Files into Azure Hulk Storage
- Insert Data into Azure Table Storage
Source: https://www.c-sharpcorner.com/article/implement-azure-file-storage-using-asp-net-core-console-application/
0 Response to "Upload File to Azure File Storage C#"
Enregistrer un commentaire