c# - 无法将 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
我不太喜欢下载到临时文件然后再次读取它的想法 感谢您的帮助。
解决方案
使用该方法OpenReadAsync()
解决了我的问题
var stream = await reference.OpenReadAsync();
推荐阅读
- javascript - 在javascript中搜索多维数组中重复值的最佳方法
- javascript - 异步/等待和承诺都没有按预期工作
- python - 如何使用一个数据框的索引在熊猫中重新索引另一个数据框
- python - 在 pd.read_csv(filename) 之后,我的行号是 NaN,为什么?
- python - 如何使用python计算直到字符串中的特定数字?
- python-3.x - 而不是在python中循环
- shell - 使用带有 curl 的密钥库
- python - 正则表达式排除后跟空格的单词
- javascript - 如何在距离一周结束的日子之后控制台记录一周中的某一天?
- reactjs - ReactJS 上下文 API 未正确更新刷新视图