无法下载Azure Blob进行流式处理

时间:2018-10-09 20:23:21

标签: c# azure .net-core containers blob

我正在使用Azure Blob存储来保存一些文件。我在下载此视频流时遇到问题,不确定为什么。我没有任何错误,只是一个空流。我已验证文件在容器中存在,甚至运行了列出容器中所有文件的代码。任何帮助将不胜感激。

private async Task<MemoryStream> GetMemoryStreamAsync(string fileName)
{
    var storageAccountName = Environment.GetEnvironmentVariable("storage_account_name");
    var storageAccountKey = Environment.GetEnvironmentVariable("storage_access_key");
    var storageContainerName = Environment.GetEnvironmentVariable("storage_container_name");

    CloudStorageAccount storageAccount = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, storageAccountKey), true);
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(storageContainerName);
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

    MemoryStream stream = new MemoryStream();

    await blockBlob.DownloadToStreamAsync(stream);

    return stream;
}

1 个答案:

答案 0 :(得分:1)

您需要在返回流之前将位置设置为零,以便流的使用者从头开始读取它。

MemoryStream stream = new MemoryStream();

await blockBlob.DownloadToStreamAsync(stream);

stream.Position = 0;

return stream;