我有一个比较简单的问题。我已经成功地将 Json 字符串消息写入 azure 服务总线上的队列。
我已经能够在broker-properties下分配一个Label,但是当我尝试分配 "ContentType "时,却没有成功。
这是我用于发送消息的代码&;设置 azure 网站上显示的属性。
'''
for json_item in resultlist:
jsonData= json.loads(json_item)
msg = Message(format(json_item).encode('utf-8'),
broker_properties = {"Label": "M1",
"ContentType": "application/json"})
sbs.send_queue_message(queue, msg)
'''
而ContentType,当我读取队列消息时,却设置为 "applicationatom+xml;type=entry;charset=utf-8"
如有任何指点,将不胜感激,谢谢。
我发现这个文档也很差。Python'azure.servicebus'库已经被重新编写了几次,并且一直在变化,所以网络上的例子非常混乱,这也没有什么帮助。使用最新的服务总线库预览版(写稿时):azure-servicebus==7.0.0b3。
我可以通过以下代码将内容类型设置为'applicationjson'(也可以设置自定义字段)。
from azure.servicebus import ServiceBusClient, Message
...
with ServiceBusClient.from_connection_string(MY_SERVICEBUS_CONN) as client:
with client.get_queue_sender(MY_QUEUE_NAME) as sender:
custom_props = {
'my_custom_field': 'yay!',
}
message = Message(json.dumps(payload))
message.user_properties = custom_props
message.properties.content_type = 'application/json'
sender.send(message)
希望能帮到你