Azure Storage – How to use Azure Files with .Net

Difficulty Level:    

In previous article Azure Storage – Azure Files, I provided the sample which use Azure PowerShell to interact with Files. In this article, I’m going to show you how to programmatically access your shares, get a list of the files on them, and upload/ download files using the .Net Storage Client Library.

I’ve created a Console Application project that will create shares, create folders, upload files, download files, list files, and delete files. And the major steps are below.

Install WindowsAzure.Storage library with Nuget Packages

Right-click the project in Solution Explorer and choose Manage NuGet Packages. Search online for “WindowsAzure.Storage” and click Install to install the Azure Storage package and dependencies.

storage-file-nuget-get

Setup storage account credential in App.config

Next, save your credentials in your project’s app.config file. Edit the app.config file so that it appears similar to the following example, replacing myaccount with your storage account name, and mykey with your storage account key:

Retrieve CloudFileClient instance

We need to get an instance of the CloudFileClient that will be used to execute requests against the File service.

Access to the file share

Create new share and folder under your storage account

Upload files to a share

I discovered there are a couple of ways to upload files to a share. Here’s one in which I use GetFileReference method.

Download files from a folder on the share

Delete files from a share

Main methods of Files REST API

Operation File Access Description
Create File Write/Delete If file is currently open in SMB, can only create the file via REST if the file has write or write/delete share mode.
Get File Read If file is currently open in SMB, can only read the file via REST if the file is open with shared read.
Set File Properties Write If file is currently open in SMB, can only set the file properties via REST if the file is open with Write sharing
Set File Metadata Write If file is currently open in SMB, can only set the file metadata via REST if file is open with Write sharing.
Delete File Delete If file is currently open in SMB, can only delete the file via REST if file is open with Delete sharing.
Put Range Write If file is currently open in SMB, can only write data against the file via REST if file is open with Write sharing.
List Ranges Read If file is currently open in SMB, can only list its ranges via REST if the file if file is open with Read sharing.

You can download demo application from here.

Son Nguyen

Son Nguyen

Son Nguyen is a Cloud Consultant working for FPT Software’s Cloud Innovation team. With deep knowledge in AWS and MS Azure, Son acts as a cloud consultant in various areas, ranging from assessment to architecture design, supporting customers from Japan, EU to US.

You may also like...

Leave a Reply

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

*