File storage is pretty much what is says in the tin. It’s a shared access file system using the SMB protocol. You can create directories, subdirectories and store files in those directories – yes, it’s a file system.
Reading a file
Using the Azure Portal either locate or create a storage account, within this create a File Storage, then create a share. Within the share upload a file, mine’s the Hello World.txt file with those immortal words Hello World within it.
Let’s read this file from our client. In many ways the client API is very similar to that used for Blob storage (as one might expect).
var storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting( "StorageConnectionString")); var fileClient = storageAccount.CreateCloudFileClient(); var share = fileClient.GetShareReference("myfiles"); var root = share.GetRootDirectoryReference(); var file = root.GetFileReference("Hello World.txt"); var contents = file.DownloadText();
Uploading a file
We can upload a file using the UploadFromFile. In this example we’ll just upload to the root folder of the myfiles share
var storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting( "StorageConnectionString")); var fileClient = storageAccount.CreateCloudFileClient(); var share = fileClient.GetShareReference("myfiles"); var root = share.GetRootDirectoryReference(); var file = root.GetFileReference("Hello World.txt"); file.UploadFromFile("Hello World.txt");
Deleting a file
Deleting files is as simple as, the following
var storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting( "StorageConnectionString")); var fileClient = storageAccount.CreateCloudFileClient(); var share = fileClient.GetShareReference("myfiles"); var root = share.GetRootDirectoryReference(); var file = root.GetFileReference("Hello World.txt"); file.Delete();
References