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

标签 c# csv azure azure-storage filehelpers

我正在尝试使用 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);

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

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

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

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

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

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

最佳答案

您必须使用BeginReadStream FileHelperAsyncEngine 的

它使用 TextReader,因此您需要执行类似的操作

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

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

关于c# - 如何使用 Filehelpers 从 Azure 文件存储读取流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41472447/

相关文章:

c# - M2MQTT如何自动重连

c# - 我可以从 null 中分辨出未定义的 Action 参数吗?

c# - 将 JQuery 转换为 ForEach() 循环会引发错误

scala - 如何使用 Scala 解析带有空列的 CSV 数据?

c# - 不使用 Azure 客户端 SDK 连接到 IoT 中心

c# - 如何获取当前函数的名称?

java - 检查 csv 文件中日期的格式

python - 在 Python Pandas read_csv 中使用多字符定界符

c# - Azure 示例 4-1-MyOrg 引发 "HttpRequestException: Invalid status code in the HttpResponseMessage: Unauthorized."错误

networking - Windows Azure : how to update service definition for existing VM role?