因此,我使用 python 和 BOTO 来访问我的 AWS SQS。我在 SQS 中有一些消息,我可以从 AWS 仪表板看到这些消息。然而,当我尝试通过 python 获取这些消息时,出现的字符只是乱码。知道这是怎么回事吗?
conn = boto.sqs.connect_to_region("us-east-1")
q = conn.get_queue('my-worker-queue')
print q
#read from message queue
message = q.read(60)
print message
print message.get_body()
鉴于上面的代码,我得到以下内容:
Queue(https://queue.amazonaws.com/247124526695/my-worker-queue)
<boto.sqs.message.Message instance at 0x16f31b8>
??e??b?+??-
消息队列中的文本如下:
hello this is a test
我想原因是base64
解码问题,如 boto 使用base64
用于消息编码和解码。您可以尝试使用get_body_encoded method http://boto.s3.amazonaws.com/ref/sqs.html#boto.sqs.message.RawMessage.get_body_encoded:
print message.get_body_encoded()
其他选项转换为原始消息 https://github.com/boto/boto/issues/831:
from boto.sqs.message import RawMessage
q.set_message_class(RawMessage)
Update
是的,你的测试用例已经很清楚了:
>>> print 'hello this is a test'.decode('base64')
??e??b?+??-
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)