Uploading data with the Azure Storage SDK

patterns & practices Developer Center

From: Developing big data solutions on Microsoft Azure HDInsight

The .NET Azure Storage Client, part of the Azure Storage library available from NuGet, offers a flexible mechanism for uploading data to the Azure blob store as files or writing data directly to blobs in an Azure blob storage container, including writing streams of data directly to Azure storage without first storing the data in local files.

The following code example shows how you can use the .NET Azure Storage Client to write data in a stream directly to a blob in Azure storage. The example is deliberately kept simple by including the credentials in the code so that you can copy and paste it while you are experimenting with HDInsight. In a production system you must protect credentials, as described in “Securing credentials in scripts and applications” in the Security section of this guide.

using System;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections.Generic;

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

namespace AzureBlobClient
{
  class Program
  {
    const string AZURE_STORE_CONN_STR = "DefaultEndpointsProtocol=https;"
          + "AccountName=storage-account-name;AccountKey=storage-account-key";

    static void Main(string[] args)
    {
      Stream Observations = GetData();

      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(AZURE_STORE_CONN_STR);
      CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
      CloudBlobContainer container = blobClient.GetContainerReference("container-name");

      var blob = container.GetBlockBlobReference("data/weather.txt");
      blob.UploadFromStreamAsync(Observations).Wait();
      Console.WriteLine("Data Uploaded!");
      Console.WriteLine("Press a key to end");
      Console.Read();
    }

    static Stream GetData()
    {
      // code to retrieve data as a stream
    }
  }
}

For information about the features of the Azure Storage Client libraries, see What’s new for Microsoft Azure Storage at TechEd 2014.

Next Topic | Previous Topic | Home | Community