Azure Containers

Can anybody tell me, why can't we create a container inside a container in azure storage? And any methods to handle, where we need to create directory hierarchy in azure storage?

Answers


You can't create a container in a container because Windows Azure simply doesn't support heirarchical containers (you should see a container as a 'disk drive' like you C:\ disk). But working with directories is supported through the CloudBlobDirectory class. Here is an example from Neil's blog:

protected void GetDirectoryList(String topLevelDirectoryName, String subDirectoryName)
{
    CloudStorageAccount cloudStorageAccount =
       CloudStorageAccount.FromConfigurationSetting(“DataConnectionString”);
    CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

    CloudBlobDirectory topLevelDirectory = cloudBlobClient.GetBlobDirectoryReferencetopLevelDirectoryName);

    CloudBlobDirectory subDirectory = topLevelDirectory.GetSubdirectory(subDirectoryName);

    IEnumerable<IListBlobItem> blobItems = subDirectory.ListBlobs();
    foreach (IListBlobItem blobItem in blobItems)
    {
        Uri uri = blobItem.Uri;
    }
}

Below is a working code for azure container from my class(used for managing blob) in a working project. Pls and pls note that your naming of the folders in the container and the blob files in the container should be in small letter or you might have an error

 using System;
 using System.Collections.Generic;
 using System.Configuration;
 using System.IO;
 using System.Linq;
 using System.Web;
 using Microsoft.WindowsAzure;
 using Microsoft.WindowsAzure.Storage;
 using Microsoft.WindowsAzure.Storage.Auth;
 using Microsoft.WindowsAzure.Storage.Blob;

  namespace XXXXXXXXXXX
{

public class Blob
{

    private CloudBlobContainer Prerequisite(string userId)
    {
        var con =   ConfigurationManager.AppSettings["StorageConnectionString"];
        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            con);

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

        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference(userId);

        return container;
    }


    public void CreateUserContainerIfNotExisting(string userId)
    {
        CloudBlobContainer container = Prerequisite(userId);

        // Create the container if it doesn't already exist.
        container.CreateIfNotExists();

        //Public access to all items in the container (meaning public can see it and download it but not modify and delete it)
        container.SetPermissions(
          new BlobContainerPermissions
          {
              PublicAccess =
                  BlobContainerPublicAccessType.Blob
          }); 
    }

    public void ReadFileInBlob(string userId)
    {
        CloudBlobContainer container = Prerequisite(userId);

        // Loop over items within the container and output the length and URI.
       // foreach (IListBlobItem item in container.ListBlobs(null, false))
        foreach (IListBlobItem item in container.ListBlobs(null, true))
        {
            if (item.GetType() == typeof(CloudBlockBlob))
            {
                CloudBlockBlob blob = (CloudBlockBlob)item;

                Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);

            }
            else if (item.GetType() == typeof(CloudPageBlob))
            {
                CloudPageBlob pageBlob = (CloudPageBlob)item;

                Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);

            }
            else if (item.GetType() == typeof(CloudBlobDirectory))
            {
                CloudBlobDirectory directory = (CloudBlobDirectory)item;

                Console.WriteLine("Directory: {0}", directory.Uri);
            }
        }
    }

    public CloudBlockBlob AddOrModifyItemToBlob(string userId, string itemKey)
    {
        CloudBlobContainer container = Prerequisite(userId);

        // Retrieve reference to a blob named "myblob".
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(itemKey);

        return blockBlob;

    }

    public void DownloadToFolderLocation(string userId, string itemKey, string location)
    {
        CloudBlobContainer container = Prerequisite(userId);

        // Retrieve reference to a blob named "photo1.jpg".
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(itemKey);

        // Save blob contents to a file.
        using (var fileStream = System.IO.File.OpenWrite(location))
        {
            blockBlob.DownloadToStream(fileStream);
        } 
    }

    public string DownloadAsStream(string userId, string itemKey)
    {
        CloudBlobContainer container = Prerequisite(userId);

        // Retrieve reference to a blob named "myblob.txt"
        CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(itemKey);

        string text;
        using (var memoryStream = new MemoryStream())
        {
            blockBlob2.DownloadToStream(memoryStream);
            text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
        }
        return text;
    }

    public void DeleteBlobFile(string userId, string itemKey)
    {
        CloudBlobContainer container = Prerequisite(userId);

        // Retrieve reference to a blob named "myblob.txt".
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(itemKey);

        // Delete the blob.
        blockBlob.Delete();
    }

  }
  }

The simplest way to create a container within another container for Azure blob storage is to use a free Azure storage management tool called Azure Explorer from Cerebrata.

It allows you to create a container by creating a new folder.

Azure Explorer


Need Your Help

Trouble Creating Excel add-in from macro

excel vba excel-vba excel-addins

I've created two VBA macro functions that worked fine when used in the current workbook. I saved as .xla and imported as an add-in. I then found and activated the add-in through the menus in exce...

Initializing issue in UIViewController

ios objective-c uiviewcontroller

I have made a custom ViewController that is MBTextViewController. When i make an instance of this class like this