Using Windows Azure Storage Service to Upload Files
Windows Azure provides a variety of cloud-based storage services like blobs, tables and queues. Moreover, the Azure SDK provides the programmatic ability to get and put files into the Azure Storage service.
To use the Azure storage service, we need to create an account. The following steps demonstrate how we can go about establishing a Windows Azure storage account.
How to Create Storage Account in Your Windows Azure Subscription
Next, select “New Storage Account” from the Azure portal toolbar in the top of the window.
You will be prompted to specify some information to create a new storage account. Specify a valid Windows Azure subscription (choose from dropdown) and a unique subdomain across all Windows Azure services.
Choose a region (in our case anywhere in US)
In my case, I chose my “Vipul’s azure plan” subscription (something I had created previously) and decided to name my storage URL as vipulstorageservice.*.core.windows.net.
It will take a few minutes to create the storage account. When it is done, you can see the status of the storage account to be “Created”.
To be able to access the storage account, we would need the storage keys which can be retrieved by clicking on the “View Access Keys” icon.
You will need access to these keys if you need to deploy files to the Windows Azure storage account.
Now, we need to create a client application which can programmatically upload files from our PC to the Windows Azure Storage.
The prerequisites for the next step is to have the latest version of Windows Azure SDK deployed on your machine where you will be create this application.
Let's start by creating a Windows console application. Start Visual Studio and create a Console application called AzureStorageDemo. Make sure the target framework is .NET Framework 4.0 and not .NET Framework 4.0 Client Profile as azure APIs are not supported on client profile.
Next, add reference to the Microsoft.WindowsAzure.StorageClient.dll which should be located in your “C:\Program Files\Windows Azure SDK\v1.5\ref\”. Note that your “Program files” might be at a different location.
Next, in your program.cs file which is auto-generated, add the following statements:
In your Main function, add the following code
static void Main(string args)
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=vipulstorageservice;AccountKey=IXOiPmB9Un0aNK4UFtarjL1nJPv/UNmYmUA5o6JHbxPlkMUEPDsL1jouLP6pvertcijiJWtzbVVYH0ySUCO4nP==");
CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference("azurecontainer");
// If the container does not exist, we need to create it.
// Let us put public permissions on the container so we can access the file from anywhere.
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
CloudBlob blob = blobContainer.GetBlobReference("testfile.txt");
blob.UploadFile(@"c:\temp\testfile.txt"); // File from local storage.
Console.WriteLine("File successfully uploaded at " + blob.Uri);
In the code above, we create an instance of CloudStorageAccount and specify the storage account details we got when we created the Azure Storage account. Then, we create a BlobClient, container for the BlobClient and specify permissions.
We then upload a local file by calling the CloudBlob.UploadFile API.
Finally, we print the URI of the upload location.
After we run the application successfully, we can verify that the file was successfully uploaded by checking the URI. In my case, the URI of the uploaded file was http://vipulstorageservice.blob.core.windows.net/azurecontainer/testfile.txt
When I open the above URI, I see that it contains the contents which were present in my local testfile.txt file. This implies that my local file was successfully copied to the Windows Azure Storage account.
If you are having problem following along, you can download the sample code from here. Note that the primary access key in the source code has been changed to protect identity of the author.
In this article, we learned how we can create a Windows Azure Storage account as well as programmatic access to the Windows Azure Storage Service. I hope you have found this information useful.
About the author
Vipul Patel is a Software Engineer currently working at Microsoft Corporation. He is currently working in the Microsoft Lync team and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at firstname.lastname@example.org
Originally published on http://www.developer.com.