How to read a stream from Azure File Store using Filehelpers

2.7k views Asked by At

I'm trying to read a csv file from Azure File Store using FileHelpers. How does this work?

I can connect to the azure file store and build a reference to te file. This is done by looping through the directories and files, in this example these are named subdirectory and filename.

Code:

//connect to storage account
CloudStorageAccount storageAccount = new CloudStorageAccount("link to account and credentials");
//connect to file client
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
//create cloudfileshare to in process
CloudFileShare inprocessShare = fileClient.GetShareReference("inprocess");
//get reference to root directory of inprocess share
CloudFileDirectory inprocessRootDir = inprocessShare.GetRootDirectoryReference();
//get reference to the subdirectory
CloudFileDirectory inprocessDir = inprocessRootDir.GetDirectoryReference(subdirectory);
//get reference to the current file 
CloudFile destfile = inprocessDir.GetFileReference(filename);

This works to get a reference to the file that I want to process.

When I use StreamReader I can use the following code and this works:

\\open stream
System.IO.Stream filepath = destfile.OpenRead()
\\read the file
System.IO.StreamReader file = new System.IO.StreamReader(filepath)

But I want to use FileHelpers, I've tried the following:

\\create async filehelper
FileHelperAsyncEngine fhe = new FileHelperAsyncEngine<MyType>();
using (fhe.BeginReadFile(filepath))

This gives the error that it only accepts a string as input. When I put in a reference to a local file like "C:\inprocess\filename.csv" it works. When I put the complete URL to the file I get the error that the connection cannot be parsed. I've also tried to map the Azure File Share to a local driveletter. This works, but I want it to be a cloud app and since it does work for StreamReader I think it should also work for FileHelpers.

1

There are 1 answers

1
Marcos Meli On BEST ANSWER

You must use the BeginReadStream of the FileHelperAsyncEngine

It uses a TextReader so you need to do something like

TextReader tr = new StreamReader(destfile.OpenRead());

using (fhe.BeginReadStream(tr))
{
   foreach(var record in fhe)
   {
   ...
   }
}