簡體   English   中英

列出所有容器和blob

[英]List all containers and blobs

我正在使用容器和blob處理Azure Local Development Storage。 我希望能夠在列表框中顯示我的所有容器和blob,就像我的本地開發存儲的樹視圖一樣。 這是我的代碼:

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;
    }

我在這里展示我的所有容器。 我需要顯示每個容器的所有blob,以及子文件夾。

您正在迭代容器,而不是容器中的blob。 在每個容器上,您需要調用ListBlobs

您的代碼將類似於:

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

很高興在這里見到你。 您不需要Listcontainer,您需要創建容器和列表blob。 首先 ,我已經告訴過你需要為本地存儲創建本地包含,如果沒有,哪里可以存儲這些文件? 你可以使用container.createIfNotExists(); 到新的,並將文件上傳到它的blob。 或下載azurestorageexplorer從azurestorageexplorer.codeplex.com,在創建本地容器azurestorageexplorer

這是我的簡單例子:

   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();
        }
    }

確保在ListBox1上設置AutoPostBack =“True”,請在此處查看更多信息表: https//azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/ 如果您有任何疑問,請保持聯系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM