如何使用 Filehelpers 从 Azure 文件存储读取流

wel*_*man 3 c# csv filehelpers azure azure-storage

我正在尝试使用 FileHelpers 从 Azure 文件存储读取 csv 文件。这是如何运作的?

我可以连接到 azure 文件存储并构建对 te 文件的引用。这是通过循环遍历目录和文件来完成的,在本例中,这些被命名为子目录和文件名。

代码:

//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);
Run Code Online (Sandbox Code Playgroud)

这可以获取对我要处理的文件的引用。

当我使用 StreamReader 时,我可以使用以下代码,这可以工作:

\\open stream
System.IO.Stream filepath = destfile.OpenRead()
\\read the file
System.IO.StreamReader file = new System.IO.StreamReader(filepath)
Run Code Online (Sandbox Code Playgroud)

但我想使用 FileHelpers,我尝试了以下方法:

\\create async filehelper
FileHelperAsyncEngine fhe = new FileHelperAsyncEngine<MyType>();
using (fhe.BeginReadFile(filepath))
Run Code Online (Sandbox Code Playgroud)

这给出了它只接受字符串作为输入的错误。当我输入对本地文件(如“C:\inprocess\filename.csv”)的引用时,它可以工作。当我将完整的 URL 放入文件时,出现无法解析连接的错误。我还尝试将 Azure 文件共享映射到本地驱动器号。这是可行的,但我希望它成为一个云应用程序,因为它确实适用于 StreamReader,我认为它也应该适用于 FileHelpers。

Mar*_*eli 5

您必须使用FileHelperAsyncEngine 的BeginReadStream

它使用 TextReader 所以你需要做类似的事情

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

using (fhe.BeginReadStream(tr))
{
   foreach(var record in fhe)
   {
   ...
   }
}
Run Code Online (Sandbox Code Playgroud)