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’

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.

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.

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.

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. //Get the reference of the Storage Blob
  10. CloudBlobClient client = storageaccount.CreateCloudBlobClient();
  11. //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
  12. CloudBlobContainer container = client.GetContainerReference("images");
  13. container.CreateIfNotExists();
  14. container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
  15. Console.WriteLine("Container got created successfully");
  16. CreateBlockBlob(container, "jpegs", "Azure");
  17. Console.WriteLine("Image has been created successfully");
  18. }
  19. static void CreateBlockBlob(CloudBlobContainer container,string strDirectoryName,string strFileName)
  20. {
  21. CloudBlobDirectory directory = container.GetDirectoryReference(strDirectoryName);
  22. CloudBlockBlob blockblob = directory.GetBlockBlobReference(strFileName + ".jpg");
  23. blockblob.UploadFromFile("d:\\Cloud-Azure.jpg");
  24. }
  25. }
  26. }
Summary

We have learned the following in this article.

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