如何从 blob 存储中读取数据并使用 azure function app 访问它

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

我的 Blob 存储中有一个 CSV 文件,我需要每天 触发 它所以我在 azure 中使用 timer trigger函数应用程序我能够在我的 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);
        }
    }
}

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

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

CsvHelper 有许多示例供您学习如何 read and write 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,应该可以解决你的问题。

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

您可以使用绑定,但您的方法可以达到同样的目的。我不认为你需要改变你的方法。可以从官方document.

学习Binding概念

你可以试试这个

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