azure-functions - 如何从 blob 存储中读取数据并使用 azure 函数应用程序访问它
问题描述
我的 Blob 存储中有一个 CSV 文件,我需要每天触发它所以我在 azure 函数应用程序中使用计时器触发器 我能够在我的 azure 函数应用程序中获取 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 文件数据并将其存储在 .xlsx 文件中
如果要读写 CSV 文件,需要安装CsvHelper
NuGet。
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 概念。
推荐阅读
- android - 如何使editText在代码中不可编辑
- r - R - 创建具有序列 c(1,4,5,8,9,12,13,16) 等的向量
- python - 文件不是 zip 文件 || 即时问题
- javascript - JavaScript:如何使用尾调用优化改进此重复功能
- r - 在 spatstat 中绘制 mppm 对象的包络时如何修改 nrank 的值?
- reactjs - 如何根据动态内容正确更新组件的 MaxHeight?
- reactjs - React:如何解决“传播类型只能从对象创建”?
- python - 更改 Seaborn Barplot 中的条形顺序,其中 y 是二分变量
- android - Jetpack Compose:将一个元素固定到另一个元素
- javascript - 刷新后如何保持localStorage值(绿色行)?