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

场景:

某设备定时于每天23:00左右将一天的运行日志.devicelogtxt上传到Azure Blob,期待Blob文件上传后, 自动通过Azure Functions 解析文件并将文件内容写入到服务总线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

Azure Functions

NuGet Package Manager

在VS Code中创建Functions步骤:

选择一个文件夹

选择C#语言

选择一个Blob触发器

Function 名称,可以保持默认

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

创建新的本地配置文件

选择创建好的storage 账户

填写要监控的容器

选择 存储账户

在当前窗口打开项目

本案例中的示例代码:

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=""});
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

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

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

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

使用VS code 创建 Azure Functions,从blob触发,解析,发送至Service Bus的更多相关文章

  1. 技术博客:Azure Functions + Azure Storage 开发

    Azure GitHub wiki 同步发布 传送门 Azure Functions 通过 Functions(一个事件驱动型无服务器计算平台,还可以解决复杂的业务流程问题)更加高效地进行开发.在本地 ...

  2. 使用 Visual Studio 开发、测试和部署 Azure Functions(一)开发

    1,什么是Azure functions Azure Functions 是 Microsoft Azure 提供的完全托管的 PaaS 服务,用于实现无服务器体系结构. Azure Function ...

  3. 利用Azure Functions和k8s构建Serverless计算平台

    题记:昨晚在一个技术社区直播分享了"利用Azure Functions和k8s构建Serverless计算平台"这一话题.整个分享分为4个部分:Serverless概念的介绍.Az ...

  4. 【Azure 应用服务】由 Azure Functions runtime is unreachable 的错误消息推导出 ASYNC(异步)和 SYNC(同步)混用而引起ThreadPool耗尽问题

    问题描述 在Azure Function Portal上显示: Azure Functions runtime is unreachable,引起的结果是Function App目前不工作,但是此前一 ...

  5. .NET 5 支持 Azure Functions OpenAPI 扩展啦

    今年5月,在 Build大会上,Azure FunctionsOpenAPI的功能支持(预览版)正式宣布. 当时,它最高支持 v3 运行时--.NET Core 3.1 版本. 最近,它发布了 .NE ...

  6. 使用 Visual Studio 开发、测试和部署 Azure Functions(二)测试,部署

    1,引言 上一篇介绍了使用使用 Visual Studio 开发 "Azure Functions" 函数,此篇介绍 “Azure Functions” 的测试以及直接从 Vist ...

  7. Windows Azure Service Bus (1) 基础

    <Windows Azure Platform 系列文章目录> 我们在基于Windows Azure进行云端开发的时候,云端的软件通常都需要与其他软件进行交互.这些其他软件可能包括其他In ...

  8. Windows Azure Service Bus (2) 队列(Queue)入门

    <Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...

  9. 【服务总线 Azure Service Bus】ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题

    Azure Service Bus 死信队列产生的原因 服务总线中有几个活动会导致从消息引擎本身将消息推送到 DLQ. 如 超过 MaxDeliveryCount 超过 TimeToLive 处理订阅 ...

随机推荐

  1. 洛谷P2634 聪聪可可 (点分治)

    ###题目链接### 题目大意: 给你一棵树,假如树上两点间的距离是 3 的倍数 的点对有 s 对,则输出最简分数  s/n ,其中 n 表示所有整棵树的点对总数. 分析: 1.显然,可以采用点分治. ...

  2. node mysql+node+express 表查询及接口建立(6)

    一.一张表查询 查询一张表在上一章节说过了,查询全部使用*,具体的就写字段名 'SELECT * FROM company' //查询所有使用* 'SELECT * FROM company WHER ...

  3. [ch03-00] 损失函数

    系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 第3章 损失函数 3.0 损失函数概论 3.0.1 概念 ...

  4. 基于Pytorch的简单小案例

    神经网络的理论知识不是本文讨论的重点,假设读者们都是已经了解RNN的基本概念,并希望能用一些框架做一些简单的实现.这里推荐神经网络必读书目:邱锡鹏<神经网络与深度学习>.本文基于Pytor ...

  5. solr 的安装和配置

    Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过Http Get操 ...

  6. Create an Embedded Framework in Xcode with Swift

    转自:http://zappdesigntemplates.com/create-an-embedded-framework-in-xcode-with-swift/ Post Series: Cre ...

  7. 设计模式GOF23(行为型模式)

    场景: – 公司里面,报销个单据需要经过流程: • 申请人填单申请,申请给经理 • 小于1000,经理审查. • 超过1000,交给总经理审批. • 总经理审批通过 – 公司里面,请假条的审批过程: ...

  8. [TimLinux] JavaScript AJAX接收到的数据转换为JSON格式

    1. 接收数据 AJAX接收数据是通过xhr.responseText属性,这是一个属性不是一个方法,这个属性得到的数据为字符串. 2. 字符串内容 当服务器发送的是一个JsonResponse({' ...

  9. CodeForces1000A-Light It Up

    B. Light It Up time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  10. R语言主成分分析(PCA)

    数据的导入 > data=read.csv('F:/R语言工作空间/pca/data.csv') #数据的导入> > ls(data) #ls()函数列出所有变量 [1] " ...