Create an application that stores a file in Windows Azure storage (Viewed - 4406 times)

Windows Azure Platform

Here I am going to create a console application that uses the Windows Azure Storage Services API. This sample code will upload a file to the Windows Azure Blob service.
 To create an application that uploads a file to a blob

  • Go to Microsoft Visual Studio 2010.
  • Click the File menu, click New, and add new Project.
  • Within the New Project dialog, navigate to Installed Templates, Visual C#, and click Windows.
  • Click Console Application. Name the project as Storageapp. Don’t forget to choose .NET Framework 4 from the drop down list and click ok.
  • From the Storageapp Project menu, click Add Reference. Within the Browse to where your Windows Azure SDK reference libraries are installed, making sure to use the latest version. For example, C:Program FilesWindows Azure SDKv1.4 ef.  Select Microsoft.WindowsAzure.StorageClient.dll and click OK (Note: if you don't have latest version please install it from Micorsoft website).
  • Copy and paste the following code to contain the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace StorageApp
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;

    namespace Storageapp
        class Program
            static void Main(string[] args)
                    CloudStorageAccount mynewcloudStorageAccount;
                    CloudBlobClient mynewblobClient;
                    CloudBlobContainer mynewblobContainer;
                    BlobContainerPermissions containerPermissions;
                    CloudBlob blob;

                    // Use the local storage account.
                    mynewcloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;

                    // If you want to use Windows Azure cloud storage account, use the following
                    // code (after uncommenting) and comment above code.
                    // mynewcloudStorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;;");

                    // Create the blob client, which provides
                    mynewblobClient = mynewcloudStorageAccount.CreateCloudBlobClient();

                    // Get the container reference.
                    mynewblobContainer = mynewblobClient.GetContainerReference("mynewcontainer");
                    // Create the container if it does not exist.

                    // Set permissions on the container.
                    containerPermissions = new BlobContainerPermissions();
                    containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;

                    // Get a reference to the blob.
                    blob = mynewblobContainer.GetBlobReference("mynewmyfile.txt");

                    // Upload a file from the local system to the blob.
                    Console.WriteLine("Starting file upload");
                    blob.UploadFile(@"c:mynewmyfilesmynewmyfile.txt");  // File from local storage.
                    Console.WriteLine("File is uploaded to " + blob.Uri);
                catch (StorageClientException e)
                    Console.WriteLine("Storage client cause an : " + e.Message);

                    // Exit the application with exit code 1.
                catch (Exception e)
                    Console.WriteLine("Error : " + e.Message);

                    // Exit the application with exit code 1.
                    // Exit the application.


  • Create a file on your local computer named c:mynewmyfilesmynewmyfile.txt .
  • blob.UploadFile(@"c:mynewmyfilesmynewmyfile.txt ");  // File from local storage.
  • Go to All Programs, click Windows Azure SDK v1.4, and click Storage Emulator.
  • On the Debug menu, click Start Without Debugging. Copy the URI displayed by the application to your browser, you can able to see the file saved in the local storage.
  • You can also store the file into Windows Azure storage account, instead of to your local storage account. In Program.cs comment the following code
  • // mynewcloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;
  • And Uncomment the following code
  • mynewcloudStorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;;");

please visit the following msdn link for more details:

Happy coding..

Post Article  Post Code 
  Post Tip    Start Forum
Quick note for developers