How to upload a blob to azure storage by REST API

How to upload a blob to azure storage by REST API

How to upload a blob to azure storage by REST API

With Azure, you get the freedom to build and deploy wherever you want, using the tools, applications, and frameworks of your choice.

AZURE STORAGE: This service is rendered by Microsoft Windows Azure for access to containers and blogs on the cloud.


Azure Blob Storage is a service for storing large amounts of unstructured data, such as text or binary data, that can be accessed from anywhere in the world via HTTP or HTTPS. You can use Blob storage to expose data publicly to the world or to store application data privately. In this article, you'll learn how to use Storage Explorer (Preview) to work with blob containers and blobs.


Storage Account: All access to Azure Storage is done through a storage account. This storage account can be a General-purpose storage account or a Blob storage account which is specialized for storing objects/blobs. See About Azure storage accounts for more information.

Container: A container provides a grouping of a set of blobs. All blobs must be in a container. An account can contain an unlimited number of containers. A container can store an unlimited number of blobs. Note that the container name must be lowercase.

Blob: A file of any type and size. Azure Storage offers three types of blobs: block blobs, page blobs, and append blobs.

Existing APIs for the tasks used by AZURE:

// Retrieve storage account from the connection string.

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(


// Create the blob client.

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.

CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob".

CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

// Create or overwrite the "myblob" blob with contents from a local file.

using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))






Why rest API's?

One question you may ask is why do you want to know how to use the REST API? Well, the Azure product team frequently releases new features (this is the firehose effect). Many times, the new features are accessible through the REST interface but haven't been surfaced through a storage client library or the UI (like the portal) yet. If you always want to use the latest and greatest, learning REST is a required skill.

Also, if you want to access blob storage with a programming language that does not have an SDK or storage client library, you can use REST instead.

Some more information on Rest:

Data can be passed in and/or out when the REST API is called. The REST API doesn’t care what it’s called from – what’s important is the information passed in the request and the data provided in the response.

Same steps have to be followed while using REST.

All storage services are accessible via REST APIs. Storage services may be accessed from within a service running in Azure, or directly over the Internet from any application that can send an HTTP/HTTPS request and receive an HTTP/HTTPS response.+


The Azure storage services support both HTTP and HTTPS; however, using HTTPS is highly recommended.

Storage Account:

All access to storage services takes place through the storage account. The storage account is the highest level of the namespace for accessing each of the fundamental services. It is also the basis for authentication.

The REST APIs for storage services expose the storage account as a resource.

Blob Service:

The Blob service provides storage for entities, such as binary files and text files. The REST API for the Blob service exposes two resources: containers and blobs. A container is a set of blobs; every blob must belong to a container.


// <summary>

/// Uploads a blob in a blob container where SAS permission is defined on a blob container using REST API.

/// </summary>

/// <param name="blobContainerSasUri"></param>

static void UploadBlobWithRestAPISasPermissionOnBlobContainer(string blobContainerSasUri)


    string blobName = "sample.txt";

    string sampleContent = "This is sample text.";

    int contentLength = Encoding.UTF8.GetByteCount(sampleContent);

    string queryString = (new Uri(blobContainerSasUri)).Query;

    string blobContainerUri = blobContainerSasUri.Substring(0, blobContainerSasUri.Length - queryString.Length);

    string requestUri = string.Format(CultureInfo.InvariantCulture, "{0}/{1}{2}", blobContainerUri, blobName, queryString);

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);

    request.Method = "PUT";

    request.Headers.Add("x-ms-blob-type", "BlockBlob");

    request.ContentLength = contentLength;

    using (Stream requestStream = request.GetRequestStream())


        requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);


   using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())