使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图1

    场景:
    某设备定时于每天23:00左右将一天的运行日志.devicelogtxt上传到Azure Blob,期待Blob文件上传后, 自动通过Azure Functions 解析文件并将文件内容写入到服务总线Service Bus的队列中。

    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图2

    上传的文件格式为:

    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图3
    步骤:

    1. 下载并安装VS Code;
    2. 下载VS Code 扩展:Azure Account/Funxtions/Nuget;
    3. 将VS Code Azure 调整成Azure-China;
    4. 在VS Code上登录Azure China账号;
    5. 下载安装Azure Functions Core Tools以便进行本地调试;
    6. 在Azrue Portal上准备Functions/Blob/Service Bus 环境;
    7. 在VS Code创建Functions;
    8. 在本地调试Functions;
    9. 使用VS Code直接发布Functions;

    本实战的完整视频:

    未命名项目.mp4 (117.06MB) 需要安装的三个扩展:
    Azure Account
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图5

    Azure Functions
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图6

    NuGet Package Manager
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图7

    在VS Code中创建Functions步骤:
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图8

    选择一个文件夹
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图9

    选择C#语言
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图10

    选择一个Blob触发器
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图11

    Function 名称,可以保持默认
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图12

    命名空间名称,可以保持默认
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图13

    创建新的本地配置文件
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图14

    选择创建好的storage 账户
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图15

    填写要监控的容器
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图16

    选择 存储账户
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图17

    在当前窗口打开项目
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图18

    本案例中的示例代码:

    1. using System;
    2. using System.IO;
    3. using Microsoft.Azure.WebJobs;
    4. using Microsoft.Azure.WebJobs.Host;
    5. using Microsoft.Extensions.Logging;
    6. using Microsoft.Azure.ServiceBus;
    7. using System.Text;
    8. using Newtonsoft.Json;
    9. namespace Company.Function
    10. {
    11. public static class BlobTriggerCSharp
    12. {
    13. [FunctionName("BlobTriggerCSharp")]
    14. public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "beifustoratgetest_STORAGE")]Stream myBlob, string name, ILogger log)
    15. {
    16. log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
    17. StreamReader reader = new StreamReader(myBlob);
    18. string msg=string.Empty;
    19. while(!string.IsNullOrEmpty(msg=reader.ReadLine()))
    20. {
    21. SendMsgToSbQueueAsync(new Msg(){dateTime=DateTime.Now,Msgstr=msg,DeviceId="001"});
    22. log.LogInformation($"oldContent:{msg}");
    23. }
    24. }
    25. public static async void SendMsgToSbQueueAsync(Msg msg)
    26. {
    27. string ServiceBusConnectionString = "Endpoint=sb://seanyutest.servicebus.chinacloudapi.cn/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=rnVwTNyXWRDhi1scJ2ukW7al/5q0Y8sNY2H01dqSl3k=";
    28. string QueueName = "test";
    29. IQueueClient queueClient = new QueueClient(ServiceBusConnectionString, QueueName);
    30. string messageBody = JsonConvert.SerializeObject(msg);
    31. var message = new Message(Encoding.UTF8.GetBytes(messageBody));
    32. await queueClient.SendAsync(message);
    33. }
    34. public class Msg
    35. {
    36. public DateTime dateTime{get;set;}
    37. public string Msgstr{get;set;}
    38. public string DeviceId{get;set;}
    39. }
    40. }
    41. }

    从本地发布到Azure
    Ctrl+shift+P
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图19

    将链接字符串配置到云端的Functions:
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图20
    其中名称要与local.settings.json中保持一致:

    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图21
    使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus - 图22

    image.png