我按照给出的详细信息将消息放入天蓝色队列中Azure Python SDK https://github.com/Azure/azure-sdk-for-python。当我尝试将消息放入队列时,
from azure.storage import QueueService
queue_service = QueueService(account_name, account_key)
queue_service.put_message('taskqueue', 'Hello world!')
消息已放入队列中,但队列为空。任何帮助将不胜感激。
好像是官方的文档 https://learn.microsoft.com/en-us/azure/storage/queues/storage-python-how-to-use-queue-storage?tabs=python#insert-a-message-into-a-queue需要更新。我们需要对文档中缺少的字符串进行编码。
下面的代码,我测试并为我工作:
from azure.storage.queue import (
QueueClient,
BinaryBase64EncodePolicy,
BinaryBase64DecodePolicy
)
...
queue_client = QueueClient.from_connection_string(
AZURE_STORAGE_CONNECTION_STRING,
QUEUE_NAME
)
# Setup Base64 encoding and decoding functions
queue_client.message_encode_policy = BinaryBase64EncodePolicy()
queue_client.message_decode_policy = BinaryBase64DecodePolicy()
message = 'Hello World'
message_bytes = message.encode('ascii')
queue_client.send_message(
queue_client.message_encode_policy.encode(content=message_bytes)
)
-
我们不能直接使用字符串queue_client.message_encode_policy.encode甚至也不简单base64.b64encode('你好')方法,因为需要类似字节的对象。
In [6]: base64.b64encode('hello')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-b1f43373737a> in <module>
----> 1 base64.b64encode('hello')
/usr/local/Cellar/[email protected] /cdn-cgi/l/email-protection/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/base64.py in b64encode(s, altchars)
...
TypeError: a bytes-like object is required, not 'str'
-
另外上面的代码使用的是最新的SDK https://learn.microsoft.com/en-us/python/api/azure-storage-queue/azure.storage.queue.queueclient?view=azure-python#send-message-content----kwargs-方法名称改变了。
-
一切都在Python 3.9下测试
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)