首页 > 解决方案 > 如何从 blob 存储中读取数据并使用 azure 函数应用程序访问它

问题描述

我的 Blob 存储中有一个 CSV 文件,我需要每天触发它所以我在 azure 函数应用程序中使用计时器触发器 我能够在我的 azure 函数应用程序中获取 Csv 文件数据

我的功能应用程序:

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

标签: azure-functionsazure-function-appazure-functions-runtimetimer-triggerazure-function-async

解决方案


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

如果要读写 CSV 文件,需要安装CsvHelperNuGet。

CsvHelper有许多示例供您学习如何读取写入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文件,我看到了这个帖子,应该可以解决你的问题。

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

您可以使用绑定,但您的方法可以达到相同的目的。我认为你不需要改变你的方法。您可以从官方文档中学习 Binding 概念。


推荐阅读