如何列出 blob 容器中的文件夹内容?
How do I list folder contents in a blob container?
我有将容器的 blob 和文件夹列出到 asp.net 列表框的代码。双击作为文件夹的 listItem 时,文件夹的内容将转到列表框。问题是这样对第一层之后的任何文件夹都不起作用,列表框反而变成空的。
我的论点是我的代码在容器中引用了目录,并且由于子文件夹不直接在容器中,因此无法识别并且列表框为空。
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"));
}
我不确定如何解决这个问题,这样当我双击时,目录引用将在必要时来自容器内,必要时在另一个文件夹内。
说到Azure blob的文件夹结构,目前是一个虚拟文件夹,访问这些虚拟文件夹中的任何东西的方式,就是将虚拟目录的名称附加到其中的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 和文件夹列出到 asp.net 列表框的代码。双击作为文件夹的 listItem 时,文件夹的内容将转到列表框。问题是这样对第一层之后的任何文件夹都不起作用,列表框反而变成空的。
我的论点是我的代码在容器中引用了目录,并且由于子文件夹不直接在容器中,因此无法识别并且列表框为空。
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"));
}
我不确定如何解决这个问题,这样当我双击时,目录引用将在必要时来自容器内,必要时在另一个文件夹内。
说到Azure blob的文件夹结构,目前是一个虚拟文件夹,访问这些虚拟文件夹中的任何东西的方式,就是将虚拟目录的名称附加到其中的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