Wednesday, April 26, 2023

Azure Series : File Share - Connecting and Reading CSV Files from Azure File Share in ASP.NET Core

 In today’s data-driven world, handling CSV files is a common task in many applications. Azure File Share provides a scalable and efficient way to store and manage these files in the cloud. In this article, we will explore how to connect to an Azure File Share and read CSV files from it using ASP.NET  Core and C#.

Prerequisites

Before we begin, ensure that you have the following prerequisites in place:

  1. An Azure account with an active subscription.
  2. A storage account and an Azure File Share created.

Setting Up Azure File Share

  1. Log in to the Azure portal (https://portal.azure.com ) and create a storage account if you haven’t already.
  2. Inside your storage account, create a new file share.
  3. Note down the storage account name, the file share name, and the storage account key. You will need these details to connect to the Azure File Share from your ASP.NET  Core application.

Creating an ASP.NET  Core Application

Now, let’s create an ASP.NET  Core application to read CSV files from Azure File Share.

  1. Open Visual Studio or your preferred code editor.
  2. Create a new ASP.NET  Core project.
  3. Install the required NuGet packages:
  4. Microsoft.Azure.Storage.File for Azure Storage File Share access.
  5. CsvHelper for CSV file parsing.
  6. Configure your Azure File Share connection in the appsettings.json file:
{
  "AzureStorageSettings": {
    "StorageAccountName": "your-storage-account-name",
    "StorageAccountKey": "your-storage-account-key",
    "FileShareName": "your-file-share-name"
  }
}
  1. Create a service to interact with Azure File Share. This service should use the Azure Storage SDK to connect to the file share.
public class AzureFileShareService
{
    private readonly IConfiguration _configuration;
    public AzureFileShareService(IConfiguration configuration)
    {
        _configuration = configuration;
    }
    public CloudFileDirectory GetFileShareDirectory()
    {
        string storageAccountName = _configuration["AzureStorageSettings:StorageAccountName"];
        string storageAccountKey = _configuration["AzureStorageSettings:StorageAccountKey"];
        string fileShareName = _configuration["AzureStorageSettings:FileShareName"];
        var storageAccount = CloudStorageAccount.Parse($"DefaultEndpointsProtocol=https;AccountName={storageAccountName};AccountKey={storageAccountKey};EndpointSuffix=core.windows.net");
        var fileClient = storageAccount.CreateCloudFileClient();
        var share = fileClient.GetShareReference(fileShareName);
        return share.GetRootDirectoryReference();
    }
    // Add methods to read CSV files here
}

Reading CSV Files from Azure File Share

Now that we have our Azure File Share service set up, let’s create a method to read CSV files:

public async Task<IEnumerable<MyDataModel>> ReadCsvFileAsync(string fileName)
{
    var directory = GetFileShareDirectory();
    var fileReference = directory.GetFileReference(fileName);
    if (await fileReference.ExistsAsync())
    {
        // Open the file stream
        var stream = await fileReference.OpenReadAsync();
        // Use CsvHelper to read the CSV data
        using (var reader = new StreamReader(stream))
        using (var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)))
        {
            var records = csv.GetRecords<MyDataModel>().ToList();
            return records;
        }
    }
    else
    {
        throw new FileNotFoundException("File not found in Azure File Share.");
    }
}

In this example, MyDataModel should be replaced with your specific CSV data model.