以下方法应将csv文件从Azure存储下载到内存流。
public async Task<AzureResponse> ReadCsvFileToStreamFromBlobAsync(CloudBlobContainer container, string fileName)
{
var ar = new AzureResponse();
// Retrieve reference to a blob (fileName)
var blob = container.GetBlockBlobReference(fileName);
if (blob != null)
{
string message;
try
{
using (var memoryStream = new MemoryStream())
{
**// code gets here and then falls right through to the end of the
//calling method, bypassing the catch portion here**
await blob.DownloadToStreamAsync(memoryStream)
**//Tried adding .ConfigureAwait(false); to the above call //downloads blob's content to a stream
//that did not work
//per this SO post, https://stackoverflow.com/questions/28526249/azure-downloadtostreamasync-method-hangs
//code doesn't reach here**
message = message = $"Csv file read to Stream correctly. Filename: {fileName}.";
ar.Status = true;
ar.Message = message;
ar.FileAsStream = memoryStream;
return ar;
}
}
catch (Exception ex)
{
message = $"Csv file was not loaded to the memory stream on {DateTime.Now} for file {fileName} with exception message {ex.Message}";
ar.Status = false;
ar.Message = message;
ar.FileAsStream = null;
return ar;
}
}
ar.Status = false;
ar.Message = $" File not found error for {fileName} on {DateTime.Now}";
ar.FileAsStream = null;
return ar;
}
注:
这个方法在我的Main方法(它是一个控制台应用程序)中用以下几行代码调用
az = new AzureStorageCommon(config);
var fileContainer = az.GetAzureFilesContainer();
var afs = new AzureFileMethods();
var returned = afs.ReadCsvFileToStreamFromBlobAsync(fileContainer, "EarningsEvents_Dec_2018.csv");
这是返回给调用方法的内容
Id=37,Status=WaitingForActivation,Method=“{null}”,Result=“{Not yet computed}”
-
是因为它是一个csv文件吗?(我怀疑)
-
-
如何使用cancellation令牌来运行它,以确保文件在代码完成之前实际完成。