简体   繁体   中英

List all containers and blobs

I am working on the Azure Local Development Storage with containers and blobs. I want to be able to display all my containers and blobs in a Listbox like a treeview of my Local Development Storage. This is my code:

public List<string> ListContainer()
    {
        List<string> blobs = new List<string>();

        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("AzureStorageConnectionString"));

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

        //Get the list of the blob from the above container

        IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers();

        foreach (CloudBlobContainer item in containers)
        {
            blobs.Add(string.Format("{0}", item.Uri.Segments[2]));
        }

        return blobs;
    }

Here i am displaying all my containers. I need to display all the blobs each container has, as well as the subfolders.

You are iterating the containers, not the blobs in the containers. On each container you need to call ListBlobs .

Your code will look something like:

foreach (CloudBlobContainer item in containers)
    {
        foreach (IListBlobItem blob in item.ListBlobs()){
            blobs.Add(string.Format("{0}", blob.Uri.Segments[2]));
        }
    }

Glad to see you here. You dont need Listcontainer, you need to create container and list blob. Firstly , I already told you that you need to create a local contain for your local Storage, if not, where can store these files? You could use container.createIfNotExists(); to new one, and upload file to its blob. Or download azurestorageexplorer from azurestorageexplorer.codeplex.com, create local container in azurestorageexplorer .

This is my simple example:

   public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        ListBox1.DataSource =ListBlob("mycontainer");
        ListBox1.DataBind();

                  }
        public List<string> ListBlob(string folder)
        {
            List<string> blobs = new List<string>();

            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

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

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

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

                    blobs.Add(string.Format("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri));

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

                    blobs.Add(string.Format("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri));

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

                    blobs.Add(string.Format("Directory: {0}", directory.Uri)); ;
                }
            }
            return blobs;
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox2.DataSource = ListBlob("mycontainer01");
            ListBox2.DataBind();
        }
    }

Make sure set AutoPostBack="True" on ListBox1, please see more information form here: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/ . Keep contact if you have any question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM