gpt4 book ai didi

Python Azure 队列,出现错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:26 28 4
gpt4 key购买 nike

我正在努力解决编码问题。我仍在尝试找出Python3的编码方案。我正在尝试将 json 对象从 Python 上传到 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'}))

关于Python Azure 队列,出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51255586/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com