Using python BOTO with AWS SQS, getting back nonse

2020-07-17 06:19发布

问题:

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?+??-