首页 > 解决方案 > 无法使用 DownloadToFileAsync 方法 C# 下载块 Blob

问题描述

我正在尝试使用 C# 从 Azure 下载块 blob。我正在使用的代码如下。

在其他测试中,我能够列出容器中的 blob,但无法下载特定的 blob。它没有给我异常或错误,但是在本地创建的文件是空的。

出于明显的原因,我已经清除了连接字符串。

我的代码看起来没问题吗?

var containerName = "samples-workitems";

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=xxxxxxxxxxxxxxxxxx.windows.net");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

CloudBlobContainer container = blobClient.GetContainerReference(containerName);

try {
  CloudBlockBlob blockBlob = container.GetBlockBlobReference("file.png");
  var localPath = string.Format("C:\\users\\user\\downloads\\file.png");



  blockBlob.DownloadToFileAsync(localPath, FileMode.Create);
  
  catch
  {
  }

标签: c#azuredownloadblob

解决方案


你知道吗 - 从昨天开始我一直坚持这个问题,自从 2 分钟前发布这个 - 我删除了......

  CloudBlockBlob blockBlob = container.GetBlockBlobReference("file.png");
  var localPath = string.Format("C:\\users\\user\\downloads\\file.png");

try 语句中的这两行代码并将它们放在它上面。现在可以用了?!

这是为什么?


推荐阅读