【问题标题】:how to read the data from blob storage and access it by using azure function app如何从 blob 存储中读取数据并使用 azure 函数应用程序访问它
【发布时间】:2020-12-31 05:01:37
【问题描述】:

我的 Blob 存储中有一个 CSV 文件,我需要每天触发它所以我在 azure function app 中使用 timer trigger 我能够获得我的天蓝色函数应用程序中的 CSV 文件数据

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

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

我的功能应用:

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

【问题讨论】:

  • CSV 文件每天更新​​吗?如果是这样,您还可以使用 BlobTrigger,然后您可以使用绑定并为自己节省访问存储帐户、容器和 blob 的额外行。

标签: azure-functions azure-function-app azure-functions-runtime timer-trigger azure-function-async


【解决方案1】:

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

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

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,应该可以解决你的问题。

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

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

【讨论】:

    【解决方案2】:

    你可以试试这个

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

    【讨论】:

      猜你喜欢
      • 2011-05-22
      • 2021-05-25
      • 1970-01-01
      • 2014-12-06
      • 2017-09-16
      • 2020-10-21
      • 1970-01-01
      • 2020-02-23
      • 2019-08-06
      相关资源
      最近更新 更多