Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access SQS message attributes using boto3

I'm trying to pass and then retrieve a message with attributes into AWS SQS. Even though I can see attributes of a message through Management Console, I can't get them using boto3, always get None. Changing "AttributeNames" doesn't make a difference. Message body can be retrieved OK.

import boto3
sqs = boto3.resource('sqs', region_name = "us-west-2")
queue = sqs.get_queue_by_name(QueueName='test')

queue.send_message(MessageBody = "LastEvaluatedKey",
                   MessageAttributes ={
                           'class_number':{
                                          "StringValue":"Value value ",
                                          "DataType":"String"
                                            }
                                        }
                    )
messages = queue.receive_messages(
                                  MaxNumberOfMessages=1,
                                  AttributeNames=['All']
                                   )
for msg in messages:
    print(msg.message_attributes) # returns None
    print(msg.body) # returns correct value
like image 230
Valera Maniuk Avatar asked Dec 01 '16 02:12

Valera Maniuk


1 Answers

Attributes (system generated) and Message Attributes (user defined) are two different kinds of things provided by the back-end API.

You're looking for message attributes, but you're asking the code to fetch attributes, instead.

AttributeNames=['All']`

It seems like you need to be fetching message attributes...

MessageAttributeNames=['All']

...when calling queue.receive_messages(). Or, the specific message attribute names that you want, if known, could be used instead of 'All' (which -- had I designed this API -- would have been called '*', but I digress).

Admittedly, this is only an intuitive guess on my part based on familiarity with the underlying API, but it seems to be consistent with http://boto3.readthedocs.io/en/latest/guide/sqs.html.

like image 167
Michael - sqlbot Avatar answered Oct 12 '22 14:10

Michael - sqlbot