如何从Asp.Net核心中的azure blob下载文件?

时间:2017-11-25 09:06:05

标签: c# azure asp.net-core azure-blob-storage

我正在尝试使用以下代码从Asp.Net Core中的azure blob下载图像。但我得到了

  

“不支持给定路径的格式”

我的代码:

StorageCredentials creds = new StorageCredentials(accountName, accountKey);
            CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
            CloudBlobClient client = account.CreateCloudBlobClient();
            container = client.GetContainerReference(blobName);

//下载

public async void DownloadAsync(string path, string[] names)
{
    string MyPath = path.Replace("https://browsercontent.blob.core.windows.net/blob1/", "");
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(MyPath+names[0]);
    path = path + names[0];
    await blockBlob.DownloadToFileAsync(path, FileMode.Create);
}

enter image description here

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

第一个参数DownloadToFileAsync to是磁盘上文件的路径,它应该是一个本地路径,如" C:\ Temp \ 3.png"例如。见下面的例子:

public async void DownloadAsync(string path, string[] names)
{
    string MyPath = path.Replace("https://browsercontent.blob.core.windows.net/blob1/", "");
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(MyPath+names[0]);
    var localPath = @"C:\Temp" + names[0];
    await blockBlob.DownloadToFileAsync(localPath , FileMode.Create);
}

其他一些提示:

  • 下次提问时添加发生异常的位置
  • 避免async void。返回public async TaskDownloadAsync(string path, string[] names)之类的任务,以便等待该方法