Why is azure file share openReadAsync() breaks when file over 4mb?

129 Views Asked by At

I'm trying to openReadAsync() csv file from azure file share. But if the file is over 4mb ReadLineAsync() returns whole file as [���������������������������], instead of returning first line.

Here is the code i use to create stream

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();

Any ideas how to debug this or what could be the problem?

1

There are 1 best solutions below

0
SaiVamshiKrishna On

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

Step-1:Uploaded .csv file in Azure File Share

enter image description here

Step-2: Here is How i am able to Read file using openReadAsync()

You need to install the CsvHelper library CsvHelper

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

Result enter image description here