Basic Usage of Azure File Storage -- Azure Storage Series: File
Haiqing Hua
I share news from Chinese website (you can use google translate please also subscribe my YouTube Channel) | Ideologist | Poet | Futurist | Educator | Technologist | Business Analyst | Data Analyst | Realtor |
Azure Storage is a cloud storage solution provided by Microsoft Azure, supporting various storage types including Blob, Queue, File, and Table.
In a previous article, I introduced the basic usage of Blob Storage. In this article, we will explore the primary usage methods of File Storage.
What is File Storage?
Azure File Storage is a service that provides cloud file sharing through the Server Message Block (SMB) protocol. Files shared via File Storage can be mounted as disks on both cloud and on-premises hosts, allowing applications to access these files through file APIs as if they were local files.
Typical use cases for File Storage include:
Structure of Azure File Storage
The basic organizational structure of File Storage is illustrated below
:
Azure Storage Account
The Storage Account is a namespace used to manage Azure Storage. It primarily controls access permissions and billing for stored data. Access to Blob, Queue, File, and Table storage services provided by Azure is managed through the Storage Account. Therefore, to use File Storage, you need to create a Storage Account first.
Share
A Share is the unit for managing shared files. Any files and directories to be shared must belong to a Share. There is no limit to the number of Shares in a Storage Account, and each Share can store any number of files. However, each Share can store up to 5TB of data.
Directory
Unlike Blob Storage, File Storage supports true file directories, allowing you to create directories as needed.
File
Files are the actual shared items, and each file can be up to 1TB in size.
URL Format
Similar to Blob Storage, each file in File Storage can be accessed via a URL. The URL format is as follows:
php
Copy code
https://<storage-account>.file.core.windows.net/<share>/<directory>/<filename>
Here's a real example:
arduino
Copy code
https://nickdemo.file.core.windows.net/demofiles/temp.txt
If you are not familiar with how to use Azure Storage Account and access Azure Storage through the WindowsAzure.Storage library, refer to the introduction in the previous article on the basic usage of Azure Table Storage.
To conveniently view the results of C# code execution, this article uses a tool released by Microsoft: Microsoft Azure Storage Explorer, referred to as Storage Explorer in the article. Below is a screenshot of File Storage in Storage Explorer:
Next, we will demonstrate how to operate File Storage through C# code.
Creating a File Share
First, we create a Share named "mylogs":
csharp
Copy code
// The CloudStorageAccount class represents an Azure Storage Account. We need to create an instance to access its resources. // Note the xxx and yyy in the connection string correspond to the Storage account name and key in Access keys. CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=yyy"); // The CloudFileClient class is a logical representation of the Windows Azure File Service client. We use it to configure and perform operations on File Storage. CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); // CloudFileShare represents a File Share object. CloudFileShare share = fileClient.GetShareReference("mylogs"); // Create the File Share if it does not exist. share.CreateIfNotExists();
Run the code above, then open Storage Explorer to see that the Share named "mylogs" has been created:
Uploading Files
File Storage supports true file directories. Before uploading files, you need to determine which directory to upload them to. Each File Share has a root directory. You can first get a reference to this root directory, then create subdirectories or directly upload files. The following code creates a subdirectory called "web" under the root directory and uploads the file "web.log" to the "web" directory:
csharp
Copy code
// Get a reference to the root directory. CloudFileDirectory rootDir = share.GetRootDirectoryReference(); // Create a reference to the subdirectory "web". CloudFileDirectory webDir = rootDir.GetDirectoryReference("web"); // Create the subdirectory "web". webDir.CreateIfNotExists(); // Create a reference to the file "web.log". CloudFile cloudFile = webDir.GetFileReference("web.log"); string localFile = @"F:\temp\web.log"; using (var fileStream = System.IO.File.OpenRead(localFile)) { // Upload the file. cloudFile.UploadFromStream(fileStream); }
Check the result in Storage Explorer:
Copying Files
Azure Storage supports copying files between Blob Storage and File Storage, but such operations involve more complex access permission management. This article only covers copying files within the same File Storage. The following code copies the "web.log" file to create "web.copy.log":
csharp
Copy code
CloudFileShare share = GetFileShare("mylogs"); CloudFileDirectory rootDir = share.GetRootDirectoryReference(); CloudFileDirectory webDir = rootDir.GetDirectoryReference("web"); CloudFile cloudFile = webDir.GetFileReference("web.log"); if (cloudFile.Exists()) { // Create a new file "web.copy.log" from "web.log". CloudFile copyFile = webDir.GetFileReference("web.copy.log"); copyFile.StartCopy(cloudFile); }
View the result of the copy operation:
Setting the Maximum Capacity of a Share
Previously, we mentioned that each Share can store up to 5TB of data. However, sometimes it may be necessary to limit this maximum value, such as allowing only up to 1TB of data:
csharp
Copy code
// Set the maximum capacity to 1024GB. share.Properties.Quota = 1024; share.SetProperties();
The code is simple. To check the maximum capacity of a Share, directly retrieve the value of the share.Properties.Quota property.
Now we have a shared directory of 1TB in the cloud. How can we use it?
Mapping a Share as a Network Drive on a Local Machine
Launch cmd.exe with administrator privileges and execute the following commands:
cmd
Copy code
cmdkey /add:<storage-account-name>.file.core.windows.net /user:<storage-account-name> /pass:<storage-account-key> net use z: \\<storage-account-name>.file.core.windows.net\mylogs
Note: Replace <storage-account-name> and <storage-account-key> in the above commands.
Then, launch cmd.exe without administrator privileges and execute the net use command again:
cmd
Copy code
net use z: \\<storage-account-name>.file.core.windows.net\mylogs
If you do not execute the net use command a second time, you will not see the drive letter in File Explorer:
Note: Ensure that the TCP port 445 used by the SMB protocol is allowed in the firewall Outbound rules.
That's it! Looks good, right?
Summary
Although we see many similarities between File Storage and Blob Storage introduced earlier, they are fundamentally different. Blob Storage essentially consists of individual files on the network, whereas File Storage implements network shared files through the SMB protocol, allowing them to be mapped as local disks by the operating system. This feature enables applications to use file operation APIs to access remote files.