无法下载 Azure Blob 进行流式传输

Acc*_*ted 2 c# containers blob azure .net-core

我正在使用 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;
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*oft 5

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

MemoryStream stream = new MemoryStream();

await blockBlob.DownloadToStreamAsync(stream);

stream.Position = 0;

return stream;
Run Code Online (Sandbox Code Playgroud)