将 Blob 和容器列出到列表框 C# 中

本文关键字:列表 Blob | 更新日期: 2023-09-27 18:34:31

我已经创建了一个带有WCF的Azure云服务,我正在尝试在其中列出我的容器和blob。我已经能够列出我的容器和 blob。我试图做的是,当我在列表框中选择一个容器时,它将显示它在另一个列表框中包含的 blob。

这是我列出容器的代码:

    public List<string> ListContainer()
    {
        List<string> blobs = new List<string>();
        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("BlobConnectionString"));
        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
        //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));
        }
            return blobs;
    }

列出我的 blob 的代码:

        public List<string> ListBlob(string folder)
    {
        List<string> blobs = new List<string>();
        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("BlobConnectionString"));
        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference(folder);

        //Get the list of the blob from the above container
        IEnumerable<IListBlobItem> blobList = container.ListBlobs();
        //Display the names on the page
        string names = string.Empty;
        foreach (IListBlobItem item in blobList)
        {
            blobs.Add(string.Format("Directory {0}", item.Uri));
        }

在我的 Web 表单中,我在页面加载中调用我的 ListContainer 方法:

 protected void Page_Load(object sender, EventArgs e)
    {
        BlobService list = new BlobService();
        ListBox2.DataSource = list.ListContainer();
        ListBox2.DataBind();
    }

当我加载项目时,我的容器列在列表框 2 中。我需要的是,当我单击容器时,它将在另一个列表框中显示它包含的 blob。我尝试了以下方法,但没有任何反应:

protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ListBox2.SelectedItem.Equals("http://127.0.0.1:10000/devstorageaccount1/mycontainer"))
        {
            BlobService list = new BlobService();
            ListBox1.DataSource = list.ListBlob("mycontainer");
            ListBox1.DataBind();
        }
        else
        {
            //Error Message
        }

将 Blob 和容器列出到列表框 C# 中

如果索引已更改,将在回发到服务器期间调用事件处理程序ListBox2_SelectedIndexChanged。为了在用户选择新项时启动回发,需要在 aspx 标记中设置 AutoPostBack="True" on ListBox2(有关详细信息,请参阅此链接(:

<asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True" 
    OnSelectedIndexChanged="ListBox2_SelectedIndexChanged" />

此外,您可能需要添加 DataTextField 来指定显示的内容,并添加 DataValueField 来指定要使用的键。

我看不到您的 ListBlob(字符串文件夹(方法的完整代码,它有一个获取本文中的 blob 的方法 https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/您可以更改 ListBlob(字符串文件夹(方法来获取结果:

public List<string> ListBlob(string folder)
        {
            List<string> blobs = new List<string>();
            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("BlobConnectionString"));
            // 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 ListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {           
                BlobService list = new BlobService();
                ListBox1.DataSource = list.ListBlob("mycontainer");
                ListBox1.DataBind();
        }

如果您有任何问题,请保持联系。