我对我的项目有疑问......我需要知道如何从Windows Azure blob存储中列出所有文件夹(在字符串列表或其他内容中)...我已经拥有了我的BlobClient以及与Azure的连接存储
谁可以帮我解决这个“问题”?
答案 0 :(得分:17)
试试这段代码。它使用 Storage Client库2.0.3 :
CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
CloudBlobContainer blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference("wad-control-container");
string blobPrefix = null;
bool useFlatBlobListing = false;
var blobs = blobContainer.ListBlobs(blobPrefix, useFlatBlobListing, BlobListingDetails.None);
var folders = blobs.Where(b => b as CloudBlobDirectory != null).ToList();
foreach (var folder in folders)
{
Console.WriteLine(folder.Uri);
}
如果您使用 Storage Client Library 1.8(即2.0之前的版本),请尝试以下代码:
var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
cloudBlobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = cloudBlobClient.GetContainerReference("wad-control-container");
IEnumerable<IListBlobItem> blobs = container.ListBlobs(new BlobRequestOptions()
{
UseFlatBlobListing = false,
});
var folders = blobs.Where(b => b as CloudBlobDirectory != null);
foreach (var folder in folders)
{
Console.WriteLine(folder.Uri);
}
答案 1 :(得分:3)
我在我的项目中使用了这个解决方案
// Retrieve reference to the container.
CloudBlobContainer container = BlobClient.GetContainerReference(lvContainer.SelectedItems[0].Text);
//Loop over VIRTUAL directories within the container
foreach (IListBlobItem item in container.ListBlobs())
{
if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory directory = (CloudBlobDirectory)item;
string[] uri = directory.Uri.ToString().Split('/');
ListViewItem dir = new ListViewItem();
dir.Text = uri[uri.Length-2];
dir.ImageIndex = 0;
ListViewItem.ListViewSubItem subitem = new ListViewItem.ListViewSubItem();
subitem.Text = String.Empty; //item.Properties.LastModifiedUtc.ToString();
dir.SubItems.Add(subitem);
subitem = new ListViewItem.ListViewSubItem();
subitem.Text = String.Empty; //item.Properties.Length + " bytes";
dir.SubItems.Add(subitem);
lvBlob.Items.Add(dir);
}
}
在我的情况下,我使用
在listView中显示结果,列出大小和日期(item.GetType() == typeof(CloudBlockBlob))
和
(item.GetType() == typeof(CloudPageBlob))
在同一个foreach中列出每个虚拟文件夹层,BlockBlobs和PageBlobs不同。 希望这会有所帮助。