Azure函数-Python-ServiceBus输出绑定-设置自定义属性

Tho*_*mas 5 python azure azureservicebus azure-functions

我有一个用Python编写的Azure函数,该函数具有Service Bus(主题)输出绑定。该功能由另一个队列触发,我们处理blobl存储中的某些文件,然后将另一个消息放入队列。

我的function.json文件如下所示:

{
"bindings": [
    {
    "type": "serviceBus",
    "connection": "Omnibus_Input_Send_Servicebus",
    "name": "outputMessage",
    "queueName": "validation-output-queue",
    "accessRights": "send",
    "direction": "out"
    }
],
"disabled": false
}
Run Code Online (Sandbox Code Playgroud)

在我的函数中,我可以像这样将消息发送到另一个队列:

with open(os.environ['outputMessage'], 'w') as output_message:
    output_message.write('This is my output test message !')
Run Code Online (Sandbox Code Playgroud)

一切正常。现在,我想向主题发送消息。我使用创建了一个订阅,SQLFilter并且需要将一些自定义属性设置为BrokeredMessage

pythonazure sdk中,我发现可以添加类似的自定义属性(我已经使用pip安装了azure模块):

from azure.servicebus import Message
sent_msg = Message(b'This is the third message',
    broker_properties={'Label': 'M3'},
    custom_properties={'Priority': 'Medium',
                        'Customer': 'ABC'}
)
Run Code Online (Sandbox Code Playgroud)

我的新function.json文件如下所示:

{
"bindings": [
    {
    "type": "serviceBus",
    "connection": "Omnibus_Input_Send_Servicebus",
    "name": "outputMessage",
    "topicName": "validation-output-topic",
    "accessRights": "send",
    "direction": "out"
    }
],
"disabled": false
}
Run Code Online (Sandbox Code Playgroud)

我已经像这样修改了我的功能:

from azure.servicebus import Message
sent_msg = Message(b'This is the third message',
    broker_properties={'Label': 'M3'},
    custom_properties={'Priority': 'Medium',
                        'Customer': 'ABC'}
)

with open(os.environ['outputMessage'], 'w') as output_message:
    output_message.write(sent_msg)
Run Code Online (Sandbox Code Playgroud)

运行该函数时,出现以下异常:

TypeError:预期为字符串或其他字符缓冲区对象

我尝试使用buffermemoryview函数,但仍然出现另一个异常:

TypeError:无法创建内存视图,因为对象没有缓冲区接口

我想知道实际的绑定是否支持BrokeredMessage以及如何处理?

mat*_*ewc 4

Python(和其他脚本语言)的 ServiceBus 输出绑定仅支持简单的字符串映射,其中您指定的字符串将成为幕后创建的 BrokeredMessage 的内容。要设置任何扩展属性或执行任何更复杂的操作,您必须在函数中自行使用 Azure Python SDK。