【问题标题】:set messagettl of a message in Azure Queue using python使用 python 在 Azure 队列中设置消息的 messagettl
【发布时间】:2021-03-04 01:12:21
【问题描述】:

我正在尝试通过发出 POST 请求并将 messagettl 指定为 -1 来使用 python3 将消息发布到 azure 队列服务,这表明消息不会过期。在文档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)

而且响应是错误的:

<?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>

我缺少哪一块拼图?

更新:

在标题中,我通过将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-3.x python-requests azure-queues


    【解决方案1】:

    我认为使用 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

    【讨论】:

    • 我不知道我怎么没在文档中看到它,但谢谢@Stanley 你救了我!
    猜你喜欢
    • 1970-01-01
    • 2019-12-06
    • 2012-06-10
    • 2019-02-14
    • 1970-01-01
    • 2013-07-13
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多