我正在使用容器和blobs处理Azure本地开发存储。我希望能够在列表框中显示我的所有容器和blobs,就像本地开发存储的树视图一样。这是我的代码:
public List<string> ListContainer()
{
List<string> blobs = new List<string>();
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//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.Segments[2]));
}
return blobs;
}这里我展示了我所有的容器。我需要显示每个容器所拥有的所有blob,以及子文件夹。
发布于 2016-02-25 04:16:30
您迭代的是容器,而不是容器中的blob。在每个容器上,您需要调用ListBlobs。
您的代码将如下所示:
foreach (CloudBlobContainer item in containers)
{
foreach (IListBlobItem blob in item.ListBlobs()){
blobs.Add(string.Format("{0}", blob.Uri.Segments[2]));
}
}发布于 2016-02-25 13:57:43
很高兴在这里见到你。你不需要Listcontainer,你需要创建容器和列表blob。首先,,我已经告诉过你,你需要为你的本地存储创建一个本地容器,如果没有,在哪里可以存储这些文件?您可以使用container.createIfNotExists();创建新的blob,并将文件上传到它的blob。或者从azurestorageexplorer.codeplex.com下载azurestorageexplorer,在azurestorageexplorer中创建本地容器。
这是我的简单示例:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.DataSource =ListBlob("mycontainer");
ListBox1.DataBind();
}
public List<string> ListBlob(string folder)
{
List<string> blobs = new List<string>();
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// 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 ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox2.DataSource = ListBlob("mycontainer01");
ListBox2.DataBind();
}
}请确保在ListBox1上设置AutoPostBack="True“,请查看此处的更多信息表单:https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/。如果你有任何问题,请保持联系。
https://stackoverflow.com/questions/35606371
复制相似问题