【问题标题】:List blobs and container into Listbox C#将 blob 和容器列表到 Listbox C#
【发布时间】:2016-02-13 14:01:24
【问题描述】:

我已经使用 WCF 创建了一个 Azure 云服务,我试图在其中列出我的容器和 blob。我已经能够列出我的容器和 blob。我想要做的是,当我在我的 ListBox 中选择一个容器时,它将显示它包含在另一个 ListBox 中的 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();

    }

当我加载项目时,我的容器列在 ListBox 2 中。我需要的是,当我单击一个容器时,它将显示它包含在另一个 ListBox 中的 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
        }

【问题讨论】:

    标签: c# wcf azure listbox


    【解决方案1】:

    如果索引已更改,将在回发到服务器期间调用事件处理程序ListBox2_SelectedIndexChanged。为了在用户选择新项目时开始回发,您需要在 aspx-markup 中的ListBox2 上设置AutoPostBack="True"(详见link):

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

    【讨论】:

    • 感谢您的快速回答,当我单击列表框中的“mycontainer”项时,我的链接出现错误:127.0.0.1:10000/devstorageaccount1/mycontainer NullReferenceException 未被用户代码处理。 WCF REST Service.dll 中出现“System.NullRefenceException”类型的异常,但未在用户代码中处理
    • @Huby03:以下帖子及其答案详细描述了 NullReferenceException 以及如何修复它:stackoverflow.com/q/4660142/642579。我希望这会有所帮助。
    【解决方案2】:

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

    【讨论】:

    • 请问如何使用它们?
    【解决方案3】:

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

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

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

    【讨论】:

    • 我正在开发本地开发存储,这就是我使用“127.0.0.1:10000/devstorageaccount1/mycontainer”的原因。我可以列出我的列表和容器,但我希望能够单击特定容器,并且它的所有 blob 都将显示在另一个列表框中,仍然无法做到,我真的卡住了!
    • 你的意思是部署到Azure存储没问题,但部署到本地模拟器时无法得到结果?
    • 我没有部署到 Azure 存储,我只需要将它上传到我的本地存储!但它不起作用,我的列表框项目没有任何我可以调用的索引,或者它有但我不知道该怎么做..
    • 你安装存储模拟器了吗?
    • 是的,我已经在本地存储中上传了一些文件,我只需要在我的 ListBox 中单击特定容器时显示它们!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2015-08-27
    • 2018-03-14
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多