Azure functions 如何从blob存储中读取数据并使用azure function app进行访问
我的Blob存储中有一个CSV文件,我每天都需要触发它,因此我在azure function app中使用计时器触发,我可以在azure function app中获取CSV文件数据Azure functions 如何从blob存储中读取数据并使用azure function app进行访问,azure-functions,azure-function-app,azure-functions-runtime,timer-trigger,azure-function-async,Azure Functions,Azure Function App,Azure Functions Runtime,Timer Trigger,Azure Function Async,我的Blob存储中有一个CSV文件,我每天都需要触发它,因此我在azure function app中使用计时器触发,我可以在azure function app中获取CSV文件数据 如何读取和写入CSV文件数据 并将其存储在.xlsx文件中 public static class Function1 { [FunctionName("Function1")] public static void Run([TimerTrig
- 如何读取和写入CSV文件数据
并将其存储在.xlsx文件中
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; } }
- 如果我需要使用绑定,我是这个概念的新手,请用一些例子来指导我
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; }
}
公共静态类函数1
{
[功能名称(“功能1”)]
公共静态无效运行([TimerTrigger(“0*/1****”)TimerInfo myTimer,ILogger日志)
{
log.LogInformation($“C#计时器触发器函数在:{DateTime.Now}执行”);
尝试
{
var ConnectionString=Environment.GetEnvironmentVariable(“AzureWebJobsStorage”);
//设置与存储帐户的连接
CloudStorageAccount-storageAccount=CloudStorageAccount.Parse(ConnectionString);
//连接到blob存储
CloudBlobClient serviceClient=storageAccount.CreateCloudBlobClient();
//连接到blob容器
CloudBlobContainer container=serviceClient.GetContainerReference(“csvfile”);
//连接到blob文件
CloudBlockBlob blob=container.GetBlockBlobReference(“empchange.csv”);
使用(var memoryStream=new memoryStream())
{
blob.DownloadToStreamAsync(memoryStream.GetAwaiter().GetResult();
memoryStream.Position=0;
使用(变量读取器=新的流读取器(memoryStream))
使用(var csv=new CsvReader(reader,CultureInfo.CurrentCulture))
{
var records=csv.GetRecords();
foreach(记录中的Foo项)
{
Console.WriteLine(项目名称);
}
}
}
}
捕获(例外情况除外)
{
控制台写入线(ex);
}
}
}
公开课Foo
{
公共int Id{get;set;}
公共int名称{get;set;}
公共整数{get;set;}
}
至于将csv文件转换为.xlsx
文件,我看到了这一点,它应该能够解决您的问题
如果我需要使用绑定,我是这个概念的新手,请用一些例子来指导我
您可以使用绑定,但您的方法可以达到相同的目的。我认为你不需要改变你的方法。您可以向官方学习绑定概念。CSV文件是否每天更新?如果是这样,您还可以使用BlobTrigger,然后使用绑定并保存访问存储帐户、容器和blob的额外行。