【问题标题】:How do I list folder contents in a blob container?如何在 blob 容器中列出文件夹内容?
【发布时间】:2019-07-16 03:11:25
【问题描述】:

我有将容器的 blob 和文件夹列出到 asp.net listBox 的代码。双击作为文件夹的 listItem 时,文件夹的内容将转到 listBox。问题是这样对第一层之后的任何文件夹都不起作用,而listBox变成了空。

我的论点是我的代码引用的目录位于容器内,并且由于子文件夹不直接位于容器内,因此无法识别它并且 listBox 为空。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "expand")
        {

            List<string> ListBlobsInFolder()
            {

                List<string> blobs = new List<string>();
                CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse("MYCONNECTIONSTRING");
                CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = cloudBlobClient.GetContainerReference("testcontainer");
                ListItem listItem = lboxLogs.SelectedItem;
                CloudBlobDirectory directory = container.GetDirectoryReference(listItem.ToString());
                IEnumerable<IListBlobItem> blobList = directory.ListBlobs();
                    foreach (IListBlobItem item in blobList)
                    {
                        blobs.Add(string.Format("{0}", item.Uri.Segments.Last()));
                    }
                return blobs;

                //IEnumerable<IListBlobItem> subDirectoryList = subDirectory.ListBlobs();
            }

            try
            {
                if (lboxLogs.SelectedItem.ToString().EndsWith("/"))
                {
                    try
                    {
                        lboxLogs.DataSource = ListBlobsInFolder();
                        lboxLogs.DataBind();
                    }
                    catch (System.NullReferenceException)
                    {
                        //do nothing
                    }
                }
            }
            catch (NullReferenceException)
            {
                //do nothing
            }
        }            
        lboxLogs.Attributes.Add("ondblclick", ClientScript.GetPostBackEventReference(lboxLogs, "expand"));
    }

我不确定如何解决这个问题,这样当我双击时,目录引用将在必要时来自容器内,并在必要时来自另一个文件夹。

【问题讨论】:

    标签: c# listbox azure-blob-storage


    【解决方案1】:

    说到Azure blobs的文件夹结构,它目前是一个虚拟文件夹,访问这些虚拟文件夹中的任何东西的方法,就是将虚拟目录的名称附加到其中的blob中,例如:

      ['Virtual-Folder/2019-02-06 10_43_04-Access Control - Microsoft Azure.png', 'Virtual-Folder/Boards.png', 'storage2.gif']
    

    当列出包含名为“Virtual-Folder”的虚拟文件夹的容器中的 blob 列表时,如果我尝试,可以通过以下名称访问其中的 blob:“'Virtual-Folder/Boards.png'”只调用“Boards.png”是行不通的。

    我还在github上用python SDK写了一些例子,欢迎大家看代码示例here

    【讨论】:

    • 感谢您的帮助。问题是我可以立即访问容器中列出的虚拟文件夹,但我不知道如何访问该文件夹中的任何文件夹。我正在从远程设备请求 blob,默认情况下它将 blob 上传到 2 个文件夹中的容器。所以,我不能硬编码任何 blob 名称。
    • 您的意思是列出虚拟文件夹的内容?如果你想这样做,你必须列出容器的内容,这将返回所有虚拟文件夹的内容。
    • 知道了——我只是使用 useFlatBlobListing: true 来规避这个问题。再次感谢。
    猜你喜欢
    • 2022-12-17
    • 1970-01-01
    • 2023-03-03
    • 2016-04-16
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    相关资源
    最近更新 更多