使用手动触发器从Blob中读取Azure Webjobs 3.0

时间:2019-10-01 12:13:29

标签: .net azure azure-webjobs azure-webjobssdk

我有一个Azure Webjob,可以随时更改blob:

        public async Task DoSomethingWithABlob(
            [BlobTrigger("myfiles/{filename}")] Stream blob,
            string filename)
        {
            //Process blob..
        }

这工作正常,但我也想在启动时手动触发Blob的处理。有办法吗?

我正在使用Azure Webjobs SDK 3.0。

1 个答案:

答案 0 :(得分:0)

据我了解,您有一个连续的Blob触发器Webjob,用于监控Blob。现在,您希望运行一次webjob,以便在此webjob启动中执行某项操作。但是,如果要监视的Blob没有更新,则不会触发您的Web作业。

实现它的方法是创建另一个手动触发的webjob来执行所需的操作,然后启动连续的blob触发的webjob。如下所示: enter image description here ConsoleApp 12是一个C#控制台应用程序,用于执行您需要的过程,并在过程完成后启动连续的Blob触发器Webjob。我为您编写了一个示例:

using Microsoft.WindowsAzure.Storage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp12
{
    class Program
    {
        static void Main(string[] args)
        {

             var containerName = "<your container name>";
             var blobName = "<blob name>";
             var StorageConnStr = "<storage account connection string>";


             CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnStr);
             var blobClient = storageAccount.CreateCloudBlobClient();
             var container = blobClient.GetContainerReference(containerName);
             var blockBlob = container.GetBlockBlobReference(blobName);
            //after get the blob , do your custom work here..




            //With all work is done ,start blobtriggerd webjob to monitor this blob
            using (var client = new WebClient())
            {
                String userName = "<webapp user name>";
                String passWord = "<webapp password>";

                client.Credentials = new System.Net.NetworkCredential(userName, passWord);

                string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" +passWord));
                client.Headers[HttpRequestHeader.Authorization] = "Basic " +credentials;
                var response = client.UploadData("<webhook url which end with start>", "POST", new byte[0]) ;

                var responseString = Encoding.Default.GetString(response);
            }



        }
    }
}

您可以在此处找到webhook网址,用户名和密码: enter image description here

有一件事,您应该知道应该将webhook url的结尾更改为:

https://<your app name>.scm.azurewebsites.net/api/continuouswebjobs/<your webjob name>/start

代替以run结尾的原始Webhook:

https://<your app name>.scm.azurewebsites.net/api/continuouswebjobs/<your webjob name>/run

因为连续的webjob没有运行功能。

这是如果我手动触发webjob的结果: 如您所见,工作已经完成(在此处输出blob内容以进行演示),并且已经开始了另一个连续的webjob,它对我来说非常理想。 enter image description here enter image description here

希望有帮助。