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

问题描述 投票:0回答:1

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

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

c# azure asp.net-core streamreader azure-file-share
1个回答
0
投票

要从 Azure 存储读取 CSV 文件的内容,可以使用 CsvHelper 库中的

CsvReader
类。

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

.csv
文件

enter image description here

第 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);
        }
    }
}

结果 enter image description here

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.