编程语言
首页 > 编程语言> > C#-Azure Blob存储列表容器和Blob

C#-Azure Blob存储列表容器和Blob

作者:互联网

我正在一个Azure存储项目上工作,在该项目中,我需要上载和下载容器中的Blob,并在列表框中列出该容器和Blob.我无法在列表框中显示容器和Blob.

这是我列出的代码:

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

解决方法:

在Web窗体中单击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;
}

与您的Web表单相比,我假设您有一个名为ListBox1的ListBox.调用方法如下:

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

标签:azure,azure-storage-blobs,c,net
来源: https://codeday.me/bug/20191027/1943889.html