使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus
場景:
某設(shè)備定時于每天23:00左右將一天的運(yùn)行日志.devicelogtxt上傳到Azure Blob,期待Blob文件上傳后, 自動通過Azure Functions 解析文件并將文件內(nèi)容寫入到服務(wù)總線Service Bus的隊列中。
?
?
上傳的文件格式為:
?
步驟:
下載并安裝VS Code;
下載VS Code 擴(kuò)展:Azure Account/Funxtions/Nuget;
將VS Code Azure 調(diào)整成Azure-China;
在VS Code上登錄Azure China賬號;
下載安裝Azure Functions Core Tools以便進(jìn)行本地調(diào)試;
在Azrue Portal上準(zhǔn)備Functions/Blob/Service Bus 環(huán)境;
?在VS Code創(chuàng)建Functions;
在本地調(diào)試Functions;
使用VS Code直接發(fā)布Functions;
?
本實(shí)戰(zhàn)的完整視頻:
?
??https://v.qq.com/x/page/m3037qoso1i.html
?
需要安裝的三個擴(kuò)展:
Azure Account
?
Azure Functions
?
NuGet Package Manager
?
在VS Code中創(chuàng)建Functions步驟:
?
選擇一個文件夾
?
選擇C#語言
?
選擇一個Blob觸發(fā)器
?
Function 名稱,可以保持默認(rèn)
?
命名空間名稱,可以保持默認(rèn)
?
創(chuàng)建新的本地配置文件
?
選擇創(chuàng)建好的storage 賬戶
?
填寫要監(jiān)控的容器
?
選擇 存儲賬戶
?
在當(dāng)前窗口打開項(xiàng)目
?
?
?
?
?
本案例中的示例代碼:
using System; using System.IO; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Host; using Microsoft.Extensions.Logging; using Microsoft.Azure.ServiceBus; using System.Text; using Newtonsoft.Json; ? namespace Company.Function {public static class BlobTriggerCSharp{[FunctionName("BlobTriggerCSharp")]public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "beifustoratgetest_STORAGE")]Stream myBlob, string name, ILogger log){log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes"); ?StreamReader reader = new StreamReader(myBlob);string msg=string.Empty;while(!string.IsNullOrEmpty(msg=reader.ReadLine())){SendMsgToSbQueueAsync(new Msg(){dateTime=DateTime.Now,Msgstr=msg,DeviceId="001"});log.LogInformation($"oldContent:{msg}");} ? ?} ? ? ?public static async void SendMsgToSbQueueAsync(Msg msg){string ServiceBusConnectionString = "Endpoint=sb://seanyutest.servicebus.chinacloudapi.cn/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=rnVwTNyXWRDhi1scJ2ukW7al/5q0Y8sNY2H01dqSl3k=";string QueueName = "test";IQueueClient queueClient = new QueueClient(ServiceBusConnectionString, QueueName); ? ?string messageBody = JsonConvert.SerializeObject(msg);var message = new Message(Encoding.UTF8.GetBytes(messageBody)); await queueClient.SendAsync(message);} ? ?public class Msg{public DateTime dateTime{get;set;}public string Msgstr{get;set;} ?public string DeviceId{get;set;}}} }?
??
?
?
從本地發(fā)布到Azure
Ctrl+shift+P
?
將鏈接字符串配置到云端的Functions:
其中名稱要與local.settings.json中保持一致:
?
微軟Azure IoT, AI,Cloud 產(chǎn)品實(shí)戰(zhàn)視頻,請關(guān)注作者公眾號:
總結(jié)
以上是生活随笔為你收集整理的使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020年银行买理财产品有风险吗?风险
- 下一篇: 【转】状态机思路在程序设计中的应用