【问题标题】:Cannot download blob storage file to memory stream无法将 blob 存储文件下载到内存流
【发布时间】:2020-01-03 13:49:19
【问题描述】:

我正在尝试将文件从 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 从内存流中读取时,内存流为空 我不太喜欢下载到临时文件然后再读一遍的想法 感谢您的帮助。

【问题讨论】:

  • 当您尝试下载到内存流时会发生什么?您遇到什么错误或问题?
  • var blobPath = $"test/test.csv}"; 在这里打错字,还是在您的实际代码中?
  • @DStanley 没有错误,内存流只是空的
  • @RonBeyer 我纠正了错字,但这不是根本问题

标签: c# azure-blob-storage


【解决方案1】:

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

var stream = await reference.OpenReadAsync();

【讨论】:

    猜你喜欢
    • 2014-07-11
    • 2015-04-23
    • 2015-12-30
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    相关资源
    最近更新 更多