gpt4 book ai didi

c# - 为什么当文件超过 4mb 时,Azure 文件共享 openReadAsync() 会中断?

转载 作者:行者123 更新时间:2023-12-03 03:21:25 30 4
gpt4 key购买 nike

我正在尝试从 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();

有什么想法如何调试这个或者可能是什么问题吗?

最佳答案

To read the content of a CSV file from Azure Storage, you can use the CsvReader class from the CsvHelper library.

第 1 步:在 Azure 文件共享中上传 .csv 文件

enter image description here

第 2 步:这是我如何使用 openReadAsync() 读取文件

You need to install the CsvHelper libraryCsvHelper

Program.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);
}
}
}

结果 enter image description here

关于c# - 为什么当文件超过 4mb 时,Azure 文件共享 openReadAsync() 会中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77196752/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com