简体   繁体   English

Azure功能blob输入与servicebus触发器绑定

[英]Azure Function blob input binding with a servicebus trigger

I'm trying to create an Azure Function that triggers on an AZ ServiceBus Queue message. 我正在尝试创建一个在AZ ServiceBus Queue消息上触发的Azure功能。 The message contains a GUID string, matching the name of a BLOB on AZ storage. 该消息包含GUID字符串,与AZ存储上的BLOB名称相匹配。 I'd like to have that BLOB available through an input binding but I'm not sure how... 我想通过输入绑定提供BLOB,但我不确定如何...

I tried : 我试过了 :

public static async Task Run(
   [ServiceBusTrigger("outgoing-mail", Connection = "QueueConnString")] string inputMessage,
   [Blob("email-messages/{inputMessage}", FileAccess.Read)] Stream mailBlob,
   [SendGrid(ApiKey = "%SendgridApiKey%")] IAsyncCollector<SendGridMessage> messageCollector,
   ILogger log)

I also tried {serviceBusTrigger} on the blob path by-the-way but either way, I get the following exception : 我还在blob路径上尝试了{serviceBusTrigger},但不管怎样,我得到以下异常:

Microsoft.Azure.WebJobs.Host: Error indexing method 'SendMailQueueWorker'. Microsoft.Azure.WebJobs.Host:错误索引方法'SendMailQueueWorker'。 Microsoft.Azure.WebJobs.Host: Unable to resolve binding parameter 'inputMessage'. Microsoft.Azure.WebJobs.Host:无法解析绑定参数'inputMessage'。 Binding expressions must map to either a value provided by the trigger or property of the value the trigger is bound to or must be a system binding expression (eg sys.randguid, sys.utcnow, etc.). 绑定表达式必须映射到触发器提供的值或触发器绑定的值的属性,或者必须是系统绑定表达式(例如sys.randguid,sys.utcnow等)。

I'm sure the input message of the queue is a string, how can I use this message content in the input binding of the BLOB? 我确定队列的输入消息是一个字符串,如何在BLOB的输入绑定中使用此消息内容?

[edit] [编辑]
I added the feature request to UserVoice, so if you're running into this issue as well, please vote! 我将功能请求添加到UserVoice,所以如果你也遇到这个问题,请投票! https://feedback.azure.com/forums/355860-azure-functions/suggestions/37528912-combine-servicebus-queue-message-with-storage-inpu https://feedback.azure.com/forums/355860-azure-functions/suggestions/37528912-combine-servicebus-queue-message-with-storage-inpu
[/edit] [/编辑]

Looks like this isn't supported for Service Bus Trigger the way it is for Queue Trigger. 服务总线触发器不支持这与队列触发器的方式不同。
You could raise a feature request on UserVoice for this. 您可以在UserVoice上为此提出功能请求。

But one thing to note is that this limitation holds only for non-JSON messages. 有一点需要注意的是,此限制适用于非JSON消息。 If you send a JSON message instead, it will get parsed as documented . 如果您发送JSON消息,它将按照文档进行解析。

Your function could be something like this 你的功能可能是这样的

using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace funk_csharp_queue
{
  public class QueueMsg
  {
    public string filename { get; set; }
  }
  public static class ServiceBusTrigger
  {
    [FunctionName("ServiceBusTrigger")]
    public static void Run(
      [ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] QueueMsg myQueueItem,
      [Blob("samples-workitems/{filename}", FileAccess.Read)] String myBlob,
      ILogger log)
    {
      log.LogInformation($"C# Service Bus trigger function processed: {JsonConvert.SerializeObject(myQueueItem)}");
      log.LogInformation($"C# Blob input read: {myBlob}");
    }
  }
}

And the message you send in the Service Bus Queue / Topic would be something like this 您在服务总线队列/主题中发送的消息将是这样的

{
  "filename": "11c8f49d-cddf-4b82-a980-e16e8a8e42f8.json"
}

Make sure you set the Content Type to application/json . 确保将内容类型设置为application/json

I don't think that's directly possible as you would like to. 我不认为你可以直接做到这一点。 I would just use a CloudBlobContainer input binding to work around this. 我只想使用CloudBlobContainer 输入绑定来解决这个问题。

[Blob("email-messages", FileAccess.Read, Connection = "BlobConnectionString")] CloudBlobContainer blobContainer

Then use the blobContainer to read the blob in your function using the inputMessage. 然后使用blobContainer使用inputMessage读取函数中的blob。

CloudBlob mailBlob = blobContainer.GetBlobReference(inputMessage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM