简体   繁体   English

列出所有容器和blob

[英]List all containers and blobs

I am working on the Azure Local Development Storage with containers and blobs. 我正在使用容器和blob处理Azure Local Development Storage。 I want to be able to display all my containers and blobs in a Listbox like a treeview of my Local Development Storage. 我希望能够在列表框中显示我的所有容器和blob,就像我的本地开发存储的树视图一样。 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. 我需要显示每个容器的所有blob,以及子文件夹。

You are iterating the containers, not the blobs in the containers. 您正在迭代容器,而不是容器中的blob。 On each container you need to call ListBlobs . 在每个容器上,您需要调用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. 您不需要Listcontainer,您需要创建容器和列表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(); 你可以使用container.createIfNotExists(); to new one, and upload file to its blob. 到新的,并将文件上传到它的blob。 Or download azurestorageexplorer from azurestorageexplorer.codeplex.com, create local container in azurestorageexplorer . 或下载azurestorageexplorer从azurestorageexplorer.codeplex.com,在创建本地容器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/ . 确保在ListBox1上设置AutoPostBack =“True”,请在此处查看更多信息表: https//azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/ Keep contact if you have any question. 如果您有任何疑问,请保持联系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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