Using python BOTO with AWS SQS, getting back nonsense characters

痞子三分冷 提交于 2019-12-06 05:44:23

问题


So, I am using python and BOTO to access my AWS SQS. I have some messages in the SQS which I can see from the AWS dashboard. However, when I try to get these messages through python, the characters that come through are just gibberish. Any idea what is going on here?

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()

Given the code above, I get the following:

Queue(https://queue.amazonaws.com/247124526695/my-worker-queue)
<boto.sqs.message.Message instance at 0x16f31b8>
??e??b?+??-

The text in the message queue is the following:

hello this is a test

回答1:


I guess the reason is base64 decoding issue, as boto uses base64 for messages encoding and decoding. You can try to use get_body_encoded method:

print message.get_body_encoded()

Other option is convert to RawMessage:

from boto.sqs.message import RawMessage
q.set_message_class(RawMessage)

Update

Yes it is, it became clear with your test case:

>>> print 'hello this is a test'.decode('base64')
??e??b?+??-


来源:https://stackoverflow.com/questions/20551216/using-python-boto-with-aws-sqs-getting-back-nonsense-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!