Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading raw messages from Amazon SQS using boto

By default, boto encodes messages with Base64 before the messages are sent to SQS. Example code:

conn = boto.connect_sqs('access_key_id', 'secret_key') 
q = conn.get_queue('myqueue')
m = Message()
m.set_body('hello!')
q.write(m)

By replacing Message() with RawMessage(), I can send raw messages to the queue without encoding. But how do I read messages from the queue without decoding? If I use the following code:

rs = q.get_messages(1)
if rs:
    m = rs[0]
    print m.get_body()

m.get_body() automatically returns the decoded result. Is there a way to retrieve raw messages?

Thanks!

like image 843
eliang Avatar asked May 22 '12 08:05

eliang


2 Answers

Actually Message class inherits from RawMessage, so it has all of it's methods. One of these methods is:

get_body_encoded()

This method is really a semi-private method used by the Queue.write method when writing the contents of the message to SQS. You probably shouldn’t need to call this method in the normal course of events.

like image 24
vartec Avatar answered Oct 14 '22 01:10

vartec


In case you are interested to just read the messages off SQS queue and the producer was someone else, you may start seeing garbage character when call get_messages(). See https://github.com/boto/boto/issues/831

Solution in this case again, is to

from boto.sqs.message import RawMessage
q.set_message_class(RawMessage)
like image 106
kk1957 Avatar answered Oct 14 '22 02:10

kk1957