我正在尝试从 azure 文件共享打开ReadAsync() csv 文件。但如果文件超过 4mb,ReadLineAsync() 将整个文件返回为 [�������������������������],而不是返回第一行。
这是我用来创建流的代码
var dir = GetCloudDirectory(type, isImport);
var cloudFile = dir.GetFileClient(fileName);
var stream = await cloudFile.OpenReadAsync();
var reader = new StreamReader(stream);
var firstLine = await reader.ReadLineAsync();
有什么想法如何调试这个或者可能是什么问题吗?
要从 Azure 存储读取 CSV 文件的内容,可以使用 CsvHelper 库中的
类。CsvReader
第 1 步:在 Azure 文件共享中上传
.csv
文件
第 2 步:这是我如何使用 openReadAsync() 读取文件
您需要安装CsvHelper库 CsvHelper
程序.cs
using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;
using CsvHelper;
using System;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string connectionString = "Your_Connection_String";
string shareName = "Your_Share_Name";
string fileName = "Your_CSV_File.csv";
try
{
ShareServiceClient serviceClient = new ShareServiceClient(connectionString);
ShareClient shareClient = serviceClient.GetShareClient(shareName);
ShareDirectoryClient directoryClient = shareClient.GetDirectoryClient("");
ShareFileClient fileClient = directoryClient.GetFileClient(fileName);
if (await fileClient.ExistsAsync())
{
Stream stream = await fileClient.OpenReadAsync();
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)))
{
// Read the CSV records and process them
var records = csv.GetRecords<dynamic>();
foreach (var record in records)
{
Console.WriteLine(record);
}
}
}
else
{
Console.WriteLine("File not found.");
}
}
catch (RequestFailedException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("An unexpected error occurred: " + ex.Message);
}
}
}
结果