繁体   English   中英

Python Azure 队列,出现错误

[英]Python Azure Queue, getting error

我正在努力解决编码问题。 我仍在尝试找出 Python3 编码方案。 我正在尝试将 Python 中的 json 对象上传到 Azure 队列。 我正在使用 Python3

我制作了 json 对象

response = {"UserImageId": 636667744866847370, "OutputImageName": "car-1807177_with_blue-2467336_size_1020_u38fa38.png"} 
queue_service.put_message(response_queue, json.dumps(response))

当它进入队列时,我收到错误

{"imgResponse":"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. ","log":null,"$return":""}

所以我必须做其他事情,因为显然我需要对我的字符串进行 base64 编码。 所以我尝试

queue_service.put_message(response_queue, base64.b64encode(json.dumps(response).encode('utf-8')))

我得到

TypeError: message should be of type str

来自 Azure 存储队列包。 如果我检查上述语句的类型,它是字节类型(有道理)。 所以我的问题是,如何将我的 json 对象编码为队列服务能够理解的内容。 我真的很希望能够保留 _ 和 - 和 . 图像名称中的字符。

如果有人想使用 QueueClient 而不是 QueueService 来解决这个问题,这里对我有用:

import json
from azure.storage.queue import QueueServiceClient, QueueClient, QueueMessage, TextBase64EncodePolicy

conn_string = '[YOUR_CONNECTION_STRING_HERE]'
queue_client = QueueClient.from_connection_string(
    conn_string,
    '[QUEUE_NAME_HERE]',
    message_encode_policy=TextBase64EncodePolicy()
)
queue_client.send_message(json.dumps({'a':'b'}))

这是我在代码中必须做的事情才能使其工作:

queue_service = QueueService(account_name=os.getenv('storageAccount'), account_key=os.getenv('storageKey'))
queue_service.encode_function = QueueMessageFormat.text_base64encode

之后,我可以放置消息:

queue_service.put_message('bbbb', message) # 'bbbb' is a queue name

暂无
暂无

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

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