更多内容,关注公众号:来学云计算

 

场景:

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

 

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

 

上传的文件格式为:

 

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

步骤:

  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;

 

本实战的完整视频:

 

  https://v.qq.com/x/page/m3037qoso1i.html

 

需要安装的三个扩展:

Azure Account

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

 

Azure Functions

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

 

NuGet Package Manager

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

 

在VS Code中创建Functions步骤:

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

 

选择一个文件夹

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

 

选择C#语言

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

 

选择一个Blob触发器

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

 

Function 名称,可以保持默认

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

 

命名空间名称,可以保持默认

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

 

创建新的本地配置文件

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

 

选择创建好的storage 账户

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

 

填写要监控的容器

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

 

选择 存储账户

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

 

在当前窗口打开项目

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

 

 

 

 

 

本案例中的示例代码:

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

 

 

 

 

 

从本地发布到Azure

Ctrl+shift+P

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

 

将链接字符串配置到云端的Functions:

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

其中名称要与local.settings.json中保持一致:

 

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

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

微软Azure IoT, AI,Cloud 产品实战视频,请关注作者公众号:

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

 

相关文章:

  • 2022-02-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2021-11-25
  • 2021-10-06
  • 2021-10-29
猜你喜欢
  • 2021-11-17
  • 2021-12-01
  • 2021-06-27
  • 2022-12-23
  • 2021-07-13
  • 2022-02-23
  • 2022-12-23
相关资源
相似解决方案