This is my fifth article about learning Azure. I will recommend going through previous four articles (links are given below) on Azure,

This article is in continuation of a previous article - “Understanding Cloud Storage in Azure”, where we created a storage account and a Blob container with the name “images”.

In this article, I will explain about how we can create Blobs in Microsoft Azure using Azure APIs in C#.

Let’s get started.

    1. Add a method called UploadBlobthat returns an EmptyResult.
    2. Within the UploadBlobmethod, get a CloudStorageAccountobject that represents your storage account information.

      Azure

    3. Get a CloudBlobClientobject represents a blob service client.
    4. Get a CloudBlobContainerobject that represents a reference to the desired blob container name.
    5. As explained earlier, Azure storage supports different blob types. To retrieve a reference to a page blob, call the GetPageBlobReferencemethod. To retrieve a reference to a block blob, call the CloudBlobContainer.GetBlockBlobReferencemethod.
    6. Once you have a blob reference, you can upload any data stream to it by calling the blob reference object's UploadFromStream.
    7. In the Solution Explorer, expand the Viewsfolder, right-click Blobs, and from the context menu, select Add->View.
    8. In the Solution Explorer, expand the Views->Sharedfolder, and open _Layout.cshtml.
    9. After the last ActionLink, add the following Html.ActionLink,
      <li>@Html.ActionLink("Upload blob", "UploadBlob", "Blobs")</li>

      Azure

I used my photo - Vipul.jpg (as shown in Visual Studio code above) as sample, for this test application and when downloaded from Azure (Blob), I got the below image,

Azure

Summary

In this article, we saw how we could upload a Blob in Azure using C#.