Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a blob storage container programmatically

I have a requirement whereby on creation of a company an associated blob storage container is created in my storageaccount with the container name set to the string variable passed in. I have tried the following:

public void AddCompanyStorage(string subDomain)
    {
        //get the storage account.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            System.Configuration.ConfigurationManager.AppSettings["StorageConnectionString"].ToString());

        //blob client now
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();            

        //the container for this is companystyles
        CloudBlobContainer container = new CloudBlobContainer("https://mystore.blob.core.windows.net/" + subDomain);
    }

This however has not created the container as I expected, am I going about this in the wrong manner? Is this possible?

like image 255
Jay Avatar asked May 12 '14 11:05

Jay


People also ask

What is difference between blob and container?

Blob and Container are two different types of storage in Azure. Blobs are a more lightweight storage option that can be used to store individual pieces of data. Containers, on the other hand, are a more robust storage option that can be used to store applications, services, and workloads.

How do I create a storage account and upload a blob?

To create a container, expand the storage account you created in the proceeding step. Select Blob Containers, right-click and select Create Blob Container. Enter the name for your blob container. See the Create a container section for a list of rules and restrictions on naming blob containers.


2 Answers

    public void AddCompanyStorage(string subDomain)
        {
            //get the storage account.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                System.Configuration.ConfigurationManager.AppSettings["StorageConnectionString"].ToString());

            //blob client now
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();            

            //the container for this is companystyles
           CloudBlobContainer container = blobClient.GetContainerReference(subDomain);

            //Create a new container, if it does not exist
           container.CreateIfNotExists();
        } 

Follow the guidline for container names:

  • A container name must be a valid DNS name, conforming to the
    following naming rules: Container names must start with a letter or
    number, and can contain only letters, numbers, and the dash (-)
    character.
  • Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted in container names.

  • All letters in a container name must be lowercase.

  • Container names must be from 3 through 63 characters long.

like image 75
NavaRajan Avatar answered Oct 21 '22 08:10

NavaRajan


CloudBlobContainer container = new CloudBlobContainer("https://mystore.blob.core.windows.net/" + subDomain);

This just creates an instance of CloudBlobContainer object. To create a container in your storage account, you would need to call CreateIfNotExists or Create function on this object.

Try this code instead:

public void AddCompanyStorage(string subDomain)
    {
        //get the storage account.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            System.Configuration.ConfigurationManager.AppSettings["StorageConnectionString"].ToString());

        //blob client now
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();            

        //the container for this is companystyles
        CloudBlobContainer container = blobClient.GetContainerReference(subDomain);
        container.CreateIfNotExists();

    }
like image 26
Gaurav Mantri Avatar answered Oct 21 '22 08:10

Gaurav Mantri