如何使用Azure函数应用程序从Blob存储中读取数据并访问数据

问题描述

我的Blob存储中有一个CSV文件,我每天都需要触发它,所以我在天蓝色功能应用程序中使用定时器触发,我的azure函数应用中的csv文件数据

  • 如何读取和写入CSV文件数据 并将其存储在 .xlsx文件

  • 我应该使用绑定吗?我是这个概念的新手,请通过一些示例指导我

我的功能应用程序:

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

    public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer,ILogger log)
    {
        log.Log@R_68_4045@ion($"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);
        }
    }
}

解决方法

如何读写CSV文件数据并将其存储在.xlsx文件中

如果要读写CSV文件,则需要安装CsvHelper NuGet。

CsvHelper有许多示例供您学习如何readwrite csv文件。我写了一个代码示例,为您读取csv文件。

Excel数据

Id,Name,Age
1,2,3

代码示例

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

关于将csv文件转换为.xlsx文件,我看到了这个post,它应该能够解决您的问题。

我需要使用绑定吗?我是这个概念的新手,请通过一些示例指导我

您可以使用绑定,但是您的方法可以达到相同的目的。我认为您无需更改方法。您可以从官方document了解绑定概念。