Share via


Create Blob Container in Azure Storage

Introduction

This article is used for creating blob container using c# code. It uses azure configuration manager , azure
storage and azure storage blob name space. I am reading storage credential from app.config

appconfig

we are using CloudStorageAccount class to configure storage account settings, CloudBlobClient class to reference of
Azure blob and CloudBlobContainer class to reference of Azure blob container.

C# Code to Create Blob Container

 
        //Create Blob if not exists
        public static string CreateBlobContainer(string connectionstring,string blobcontainername)
        {
            try
            {
                
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobcontainername);
                if (blobContainer.Exists())
                    return "Container already exists";
                blobContainer.CreateIfNotExists();
                blobContainer.SetPermissions(new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });
                return "Container created";
            }
            catch(Exception ex)
            {
                return "Error !!!" + ex.Message;
            }
            
        }
    

Required Namespace to Create Blob Container

 
      using Microsoft.Azure; // Namespace for Azure Configuration Manager
     using Microsoft.WindowsAzure.Storage; // Namespace for Storage Client Library
       using Microsoft.WindowsAzure.Storage.Blob; // Namespace for Blob storage
        using Microsoft.WindowsAzure.Storage.File; // Namespace for File storage
        using Microsoft.WindowsAzure.Storage.Auth;
  

Calling of method

 
      string strconnection = CloudConfigurationManager.GetSetting("StorageConnectionString");
        Console.WriteLine("Blob container ready to create ");
        Console.WriteLine(CreateBlobContainer(strconnection, "myblobcontainer"));
   

Points of Interest

This article explain each step to create blob container using c#

History

No updates available