首页 > 解决方案 > 无法将 blob 存储文件下载到内存流

问题描述

我正在尝试将文件从 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 我不太喜欢下载到临时文件然后再次读取它的想法 感谢您的帮助。

标签: c#azure-blob-storage

解决方案


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

var stream = await reference.OpenReadAsync();

推荐阅读