Creating Azure Functions use VS code, from blob trigger, resolution, sent to the Service Bus

More interested in the public numbers: to learn cloud computing

 

Scenes:

The scheduled around 23:00 a day to day running log .devicelogtxt uploaded to Azure Blob, looking after the Blob file upload, automatically written to the Service Bus Service Bus by Azure Functions parse the file and the contents of the file in the queue.

 

 

Upload file format:

 

step:

  1. Download and install the VS Code;

  2. Extended download VS Code: Azure Account / Funxtions / Nuget;

  3. Adjusted to the VS Code Azure Azure-China;

  4. Log Azure China accounts on the VS Code;

  5. Download and install Azure Functions Core Tools for local debugging;

  6. Preparation Functions / Blob / Service Bus environment on Azrue Portal;

  7.  Functions created in VS Code;

  8. Debugging Functions locally;

  9. Use VS Code immediate release Functions;

 

Full video of this combat:

 

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

 

We need to install three extensions:

Azure Account

 

Azure Functions

 

NuGet Package Manager

 

Functions created in step VS Code:

 

Select a folder

 

Select the C # language

 

Select a trigger Blob

 

Function names, you can keep the default

 

Namespace name, you can keep the default

 

Create a new local profile

 

Select Create a good storage account

 

Fill the container to be monitored

 

Select the storage account

 

Open the project in the current window

 

 

 

 

 

The sample code in this case:

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

 

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

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

 

 

Guess you like

Origin www.cnblogs.com/shuzhenyu/p/12065828.html