[英]How to use a service principal in azure function (python) to listen to azure service bus
[英]Azure Function app - Python SDK - How to read Service Bus Message body
我完成了Azure解決方案:Azure Function,服務總線,Python SDK代碼。 我的 Function 是由服務總線消息觸發的。 Function 按預期觸發。 Function 代碼如下
import logging
import json
import requests
import azure.functions as func
def main(msg: func.ServiceBusMessage):
logging.info('Python ServiceBus queue trigger processed message: %s',
msg.get_body().decode('utf-8'))
url = 'WebHookURL'
payload = {"keyA": "valueA"}
requests.post(url, data=json.dumps(payload), headers={'Content-Type': 'application/json'})
問題是如何獲取服務總線消息嵌套值。 在 Ms 文檔中,我在下面找到了一個示例,但是如何獲取消息body
詳細信息( 'body': msg.get_body().decode('utf-8')
)。 消息body
是解決方案的核心,因為在這個消息body
中有很多外部服務傳遞的key:value
屬性。
def main(msg: func.ServiceBusMessage):
logging.info('Python ServiceBus queue trigger processed message.')
result = json.dumps({
'message_id': msg.message_id,
'body': msg.get_body().decode('utf-8'),
'content_type': msg.content_type,
'expiration_time': msg.expiration_time,
'label': msg.label,
'partition_key': msg.partition_key,
'reply_to': msg.reply_to,
'reply_to_session_id': msg.reply_to_session_id,
'scheduled_enqueue_time': msg.scheduled_enqueue_time,
'session_id': msg.session_id,
'time_to_live': msg.time_to_live,
'to': msg.to,
'user_properties': msg.user_properties,
'metadata' : msg.metadata
}, default=str)
我完成了 Function App Python 代碼,如下所示,按預期工作,經過驗證和測試。
import logging
import json
import azure.functions as func
def main(msg: func.ServiceBusMessage):
result = ({
'body': json.loads(msg.get_body().decode('utf-8'))
})
try:
resource_name = result.get('body', {}).get('resourceName')
logging.info('resourceName: %s', resource_name)
except Exception as e:
logging.info(e)
輸入 json 內容:
{
"resourceName":"testVM",
"resourceVnet": "Vnet01"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.