简体   繁体   English

使用 python 在 Azure 队列中设置消息的 messagettl

[英]set messagettl of a message in Azure Queue using python

I'm trying to post a message to azure queue service using python3 by making a POST request and specifying messagettl to -1 which indicates the message does not expire.我正在尝试通过发出 POST 请求并将messagettl指定为-1来使用 python3 将消息发布到 azure 队列服务,这表明消息不会过期。 In the doc https://docs.microsoft.com/en-us/rest/api/storageservices/put-message I have to specify the Authorization key and Date which indicates the time at which the response was initiated (both parameters are required), and the body must be an XML, here what I did:在文档https://docs.microsoft.com/en-us/rest/api/storageservices/put-message我必须指定Authorization密钥和Date ,指示响应启动的时间(两个参数都是必需的),并且主体必须是 XML,这里我做了什么:

url = "https://MyStorageAccountName.queue.core.windows.net/MyQueueName?messagettl=-1"
xml = """<?xml version='1.0' encoding='utf-8'?>
<QueueMessage>  
<MessageText>First message</MessageText>  
</QueueMessage> """

headers = {'Content-Type': 'application/xml',
'Authorization' : 'SharedKey MyStorageAccountName:MyKey1....==',
'Date' : str(datetime.utcnow())}

print(requests.post(url, data=xml, headers=headers).text)

And the response is an error:并且响应是一个错误:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
   <Code>AuthenticationFailed</Code>
   <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:44d1fd4c-c003-001d-215...000
Time:2020-11-20T15:39:10.9730253Z</Message>
   <AuthenticationErrorDetail>The Date header in the request is incorrect.</AuthenticationErrorDetail>
</Error>

which piece of the puzzle I am missing?我错过了哪一块拼图?

UPDATE:更新:

In headers I fixed the issue by replacing str(datetime.utcnow()) with format_date_time(mktime(datetime.now().timetuple())) and fixed the related date error, but I have a new error and don't know how to sign my key:在标题中,我通过用format_date_time(mktime(datetime.now().timetuple()))替换str(datetime.utcnow())解决了这个问题并修复了相关的日期错误,但我有一个新错误,不知道如何签署我的密钥:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
   <Code>AuthenticationFailed</Code>
   <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:359305a5-a003-0034...
Time:2020-11-20T15:59:12.4611176Z</Message>
   <AuthenticationErrorDetail>The MAC signature found in the HTTP request 'HACSNj/4PwH...MyKey...YJQ==' is not the same as any computed signature. Server used following string to sign: 'POST

application/xml
Fri, 20 Nov 2020 15:59:09 GMT
/MystorageAccount/MyQueueName'.</AuthenticationErrorDetail>
</Error>

I think using python SDK to do this is much easier, just try the code below:我认为使用 python SDK 来做到这一点要容易得多,只需尝试以下代码:

from azure.storage.queue import QueueClient

connectionString = "<storage account connection string>"
queueName = "<queue name>"

queueClient = QueueClient.from_connection_string(connectionString, queueName)

queueClient.send_message(content = 'hello sdk', time_to_live=-1)

Result:结果:

在此处输入图片说明

For info about python queue client sdk, just refer to this doc .有关 python 队列客户端 sdk 的信息,请参阅此文档

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

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