how to read the data from blob storage and access it by using azure function app

10k views Asked by At

I have a CSV file in my Blob Storage and I need to trigger it every day So I am using timer trigger in azure function app I am able to get the Csv file Data in my azure function-app

  • how to read and write the CSV-file data and store it in .xlsx file

  • should I need to Use bindings I am new to this Concept please guide me on this with some Examples

My function App:

public static class Function1`
{
   
  [FunctionName("Function1")]

    public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
       
        try
        {
          
            var ConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            // Setup the connection to the storage account
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
            // Connect to the blob storage
            CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
            // Connect to the blob container
            CloudBlobContainer container = serviceClient.GetContainerReference("csvfile");
            // Connect to the blob file
            CloudBlockBlob blob = container.GetBlockBlobReference("empchange.csv");
            // Get the blob file as text
            string contents = blob.DownloadTextAsync().Result;

           
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex);
        }
    }
}
2

There are 2 answers

1
Frank Borzage On BEST ANSWER

how to read and write the CSV-file data and store it in .xlsx file

If you want to read and write CSV files, you need to install CsvHelper NuGet.

CsvHelper has many examples for you to learn how to read and write csv files. I wrote a code sample for reading csv files for you.

Excel Data

Id,Name,Age
1,2,3

Code Sample

    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            try
            {
                var ConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
                // Setup the connection to the storage account
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
                // Connect to the blob storage
                CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
                // Connect to the blob container
                CloudBlobContainer container = serviceClient.GetContainerReference("csvfile");
                // Connect to the blob file
                CloudBlockBlob blob = container.GetBlockBlobReference("empchange.csv");
                
                using (var memoryStream = new MemoryStream())
                {
                    blob.DownloadToStreamAsync(memoryStream).GetAwaiter().GetResult();
                    memoryStream.Position = 0;
                    using (var reader = new StreamReader(memoryStream))
                    using (var csv = new CsvReader(reader, CultureInfo.CurrentCulture))
                    {
                        var records = csv.GetRecords<Foo>();
                        foreach (Foo item in records)
                        {
                            Console.WriteLine(item.Name);
                        }
                    }

                }
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex);
            }
        }
    }


    public class Foo
    {
        public int Id { get; set; }
        public int Name { get; set; }
        public int Age { get; set; }
    }

As for converting the csv file into .xlsx file, I saw this post, it should be able to solve your problem.

should I need to Use bindings I am new to this Concept please guide me on this with some Examples

You can use binding, but your approach can achieve the same purpose. I don't think you need to change your approach. You can learn Binding concept from the official document.

0
Biddut On

you can try this

using (var memoryStream = new MemoryStream())
            {
                //downloads blob's content to a stream
                blockBlobReference.DownloadToStreamAsync(memoryStream).GetAwaiter().GetResult();

                //puts the byte arrays to a string
                text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
            }