使用 python 在 Azure Queue 中设置消息的 messagettl
set messagettl of a message in Azure Queue using python
我正在尝试 post 使用 python3 向 azure queue 服务发送消息,方法是发出 POST 请求并指定 messagettl
到 -1
表示消息没有过期。在文档 https://docs.microsoft.com/en-us/rest/api/storageservices/put-message 中,我必须指定 Authorization
键和 Date
指示启动响应的时间(两个参数都是必需的),并且 body 必须成为 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)
响应错误:
<?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>
我漏掉了哪一块拼图?
更新:
在 headers 中,我通过将 str(datetime.utcnow())
替换为 format_date_time(mktime(datetime.now().timetuple()))
解决了问题并修复了相关的日期错误,但我有一个新错误并且不知道如何签署我的键:
<?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>
我认为使用 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)
结果:
关于python队列客户端sdk,请参考this doc。
我正在尝试 post 使用 python3 向 azure queue 服务发送消息,方法是发出 POST 请求并指定 messagettl
到 -1
表示消息没有过期。在文档 https://docs.microsoft.com/en-us/rest/api/storageservices/put-message 中,我必须指定 Authorization
键和 Date
指示启动响应的时间(两个参数都是必需的),并且 body 必须成为 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)
响应错误:
<?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>
我漏掉了哪一块拼图?
更新:
在 headers 中,我通过将 str(datetime.utcnow())
替换为 format_date_time(mktime(datetime.now().timetuple()))
解决了问题并修复了相关的日期错误,但我有一个新错误并且不知道如何签署我的键:
<?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>
我认为使用 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)
结果:
关于python队列客户端sdk,请参考this doc。