Silver Award WinnerSilver Award Winner


Introduction

Microsoft Azure storage provides a low cost service in the cloud for a couple of cents per Gigabit. The service provides ability to store blobs (documents, media), table (NoSQL structured data), queues (reliable messaging), and file (shared storage for legacy applications).

In this short article a demonstration how to upload a file (word document) as a blob to Azure storage using .NET code.

Scenario

Assume you have a requirement to store a file on Azure Blob Storage from a Service, WebJob or custom code in an on premise solution. The diagram below shows the scenario described in the next paragraph.


Picture 1. High level overview.

Set up

A storage account is created in an Azure Subscription with a container named files.

Walkthrough

In following steps the scenario will be described how a file from a file location is loaded into a blob storage container. The Nuget packages for Azure Storage client and Configuration Manager are installed in the Windows Forms application project.





Picture 2. Installing the NuGet Packages.

The following code has been implemented to enable uploading a chosen file to the designated blob storage container i.e. files.

When the application starts the user can click File and select a file in a specific folder.

 Picture 3. Select a document.

Subsequently click UploadToBlobStorage and see if the file is uploaded.

 Picture 4. Upload the document.

Browsing in Azure Portal shows that file is present in the files container in the tnwiki storage account.



Picture 5. Show the document in Azure Storage container tnwiki.

Explanation

To programmatically store a document to Azure Blob Storage you will need to install the two Nuget packages as shown in the walkthrough. The following using statements are required in your program.

The implementation uses the CloudConfiguration manager to read the application settings. In case you want to upload your code to Azure to for instance run your code inside a WebJob the CloudConfiguration read the settings from the Web App settings. When running the code locally it will read the settings from app.config.

The code below shows how to upload a selected document to Azure Storage.

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("files");
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(Path.GetFileName(filePath));
// Create or overwrite the "myblob" blob with contents from a local file.
blockBlob.UploadFromFile(filePath, System.IO.FileMode.Open);

The CloudStorage object requires a connection string to connect to Azure Storage. The connectionstring looks like:

DefaultEndpointsProtocol=https;AccountName=tnwiki;AccountKey=07UcNDjWMdDh5FbgvDUvopU1nk4sCvYvpWSDlM8dvqHSRk1yxB+Re0R3TrvC86w7ykMUmWrltFyP2S2uJB/QTQ==

A ContainerBlobClient is created from the storageAccount and client will get a reference to the container specified in the GetContainerReference method. Next a reference is obtained if present from a blob or a reference is created by GetBlobReference method. Finally the document is uploaded from a file location i.e. filePath.

See Also

Other suggested reading material: