Azure Blob存储列表容器和blob

时间:2016-02-10 17:05:53

标签: c# .net azure azure-storage-blobs

我正在开发一个Azure存储项目,我需要在容器中上传和下载blob,并在列表框中列出容器和blob。我无法在列表框中显示容器和blob。

这是我的List代码: enter image description here

最后是我调用上传,下载和列表方法的界面背后的代码:

enter image description here

2 个答案:

答案 0 :(得分:3)

当您点击网络表单中的Button3时,您没有看到任何结果的原因是您没有从ListBlob方法中获取任何数据。

更改ListBlob方法以返回如下结果:

public List<string> GetBlobs()
{
    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("mycontainer");

    // 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;
}

在你的webform中,我假设你有一个名为ListBox1的ListBox。调用方法如:

protected void Button3_Click(object sender, EventArgs e)
{
    ListBox1.DataSource = GetBlobs();
    ListBox1.DataBind();
}

答案 1 :(得分:1)

我不清楚你正在经历什么问题,因为你还没有完全解释。在this示例中提取的以下代码中演示了在容器中列出包含分页支持的blob。

BlobContinuationToken token = null; 
do 
{ 
BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token); 
token = resultSegment.ContinuationToken; 
foreach (IListBlobItem blob in resultSegment.Results) 
{ 
// Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory 
Console.WriteLine("{0} (type: {1}", blob.Uri, blob.GetType()); 
} 
} while (token != null);