Azure Storage Account - Creating Block Blob Programmatically Using C#

In this article, we will learn how to create a Block Blob (an image) programmatically in a storage account.

Please go through the following articles to learn more about Storage Account.

  1. Azure Storage – Basics
  2. Azure Resource Manage Template: Create A Storage Account Using Blank Template
  3. Create a Storage Account and learn how to access It Programmatically
  4. Azure Storage - Creating Blob Container Using Storage Client Library
  5. Azure Storage Account Why Two Access Keys

In the above articles, we have learned how to create a Storage Account and perform a few basic operations to the Storage Services. We have also learned how to create a ConnetionString that could be used to connect to the Storage Account using Microsoft .NET Client Storage Library.

In this article, we will learn the following.

  • Create a folder named ‘jpegs’ in an existing Blob Container named ‘images’.
  • Upload an image to the ‘jpegs’ folder.

Create a folder named ‘jpegs’ in an existing Blob Container named ‘images’

Navigate to the ‘images’ container, as shown below. Currently, there are no items in this container.


Note Currently, there is no facility in the Azure Management portal for creating a folder. Below are the steps required for creating the folder.

  • Get the reference of the Blob Container
  • Get the reference of the directory using the GetDirectoryReference function of the CloudBlobContainer classs.

Below is the code for creating a directory.

  1. CloudBlobDirectory directory = container.GetDirectoryReference(strDirectoryName);  
Please note that you cannot create a directory alone in the Container using Storage Client Library. You need to accompany it along with a Blob. Let’s see how to do that now.

Upload an image (block blob) to the ‘jpegs’ folder.

Below are the steps required for creating the Folder.
  • Get the reference of the CloudBlobDirectory.
  • Get the reference of the CloudBlockBlob using the GetBlockBlobReference function of the CloudBlobDirectory class.

Below is the code for creating the Blob (image).

  1. CloudBlockBlob blockblob = directory.GetBlockBlobReference(strFileName + ".jpg");  
  2. blockblob.UploadFromFile("d:\\Cloud-Azure.jpg");  
There are multiple ways of uploading the files to the Blob. The above example directory reads the file from the hard disk and uploads the same to the Blob.

Execute the application by pressing Ctrl + F5.



As per the above screen capture, the image got uploaded successfully. Let’s navigate to the Storage Account to review the changes.

Clicking on the Container took me to the following blade where all the items of the container are listed.


Click on the "jpegs" folder to view the content of this folder.



As shown above, the image got uploaded successfully. Click on the Azure.jpg Blob to navigate to its properties, shown below.



You can do the following actions from the "Blob Properties" blade.
  • Download
    Clicking on the "Download" button will download the files to the local hard disk.

  • Delete
    Clicking on the "Delete" button will delete the file from the Blob Storage.

  • View the image
    Copy the path of the blob and view the same in a browser, as shown below.



    Please note that I’m able to browse the image URL in the browser as I set the permission of the container to allow access to all its contents to public, using the following line of code.
    1. container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });  

Below is the complete code.

  1. namespace StorageAccount  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             //Get the reference of the Storage Account  
  8.             CloudStorageAccount storageaccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["ConnectionString"].ToString());  
  9.   
  10.             //Get the reference of the Storage Blob  
  11.             CloudBlobClient client = storageaccount.CreateCloudBlobClient();  
  12.   
  13.             //Get the reference of the Container. The GetConainerReference doesn't make a request to the Blob Storage but the Create() & CreateIfNotExists() method does. The method CreateIfNotExists() could be use whether the Container exists or not  
  14.             CloudBlobContainer container = client.GetContainerReference("images");  
  15.             container.CreateIfNotExists();  
  16.             container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });  
  17.   
  18.             Console.WriteLine("Container got created successfully");  
  19.   
  20.             CreateBlockBlob(container, "jpegs""Azure");  
  21.             Console.WriteLine("Image has been created successfully");  
  22.   
  23.         }  
  24.         static void CreateBlockBlob(CloudBlobContainer container,string strDirectoryName,string strFileName)  
  25.         {  
  26.             CloudBlobDirectory directory = container.GetDirectoryReference(strDirectoryName);  
  27.             CloudBlockBlob blockblob = directory.GetBlockBlobReference(strFileName + ".jpg");  
  28.             blockblob.UploadFromFile("d:\\Cloud-Azure.jpg");  
  29.         }  
  30.           
  31.     }  
  32. }  
Summary

We have learned the following in this article.
  • Create a directory
  • Create a file into a directory
  • Delete and download the Block Blob from Azure Management portal
  • View the properties
  • Programmatically set the container permissions to public access.

Hope, you enjoyed reading the article. Your feedback is appreciated.