无法将Blob存储文件下载到内存流

时间:2020-01-03 13:49:19

标签: c# azure-storage-blobs

我正在尝试将文件从Blob存储下载到流中。下面的代码有两件事:下载到文件和下载到流,仅下载到文件部分起作用。

            var connectionString = "..."
            var fileMetaData = await dbService.GetFileMetaDataAsync(fileId);
            var storageAccount = CloudStorageAccount.Parse(connectionString);
            var blobClient = storageAccount.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference(fileMetaData.ProjectCode);
            var blobPath = $"test/test.csv";
            var reference = container.GetBlobReference(blobPath);
            var memoryStream = new MemoryStream();
            try
            {

                await reference.DownloadToStreamAsync(memoryStream);
                await reference.DownloadToFileAsync("C:\\temp\\test.csv", FileMode.Create);
            }
            catch (RequestFailedException)
            {
                logger.LogError($"Cannot download blob at {blobPath}");

                throw;
            }

当我尝试使用StreamReader读取内存流时,该内存流为空 我真的不喜欢下载到临时文件然后再次阅读的想法 感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用方法OpenReadAsync()解决了我的问题

var stream = await reference.OpenReadAsync();