Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SQS boto3 send_message returns 'dict' object has no attribute 'send_message'

  • I am new to python and I am still learning it.
  • I am writing a python code to send a message to AWS SQS
  • Following is the code I have written

    from boto3.session import Session session = Session(aws_access_key_id='**', aws_secret_access_key='**', region_name='us-west-2') clientz = session.client('sqs') queue = clientz.get_queue_url(QueueName='queue_name') print queue responses = queue.send_message(MessageBody='Test') print(response.get('MessageId'))

    • On running this code it returns

    {u'QueueUrl': 'https://us-west-2.queue.amazonaws.com/@@/queue_name', ' ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '@@'}}

    Traceback (most recent call last): File "publisher_dropbox.py", line 77, in responses = queue.send_message(MessageBody='Test')

    AttributeError: 'dict' object has no attribute 'send_message'

  • I am not sure what the 'dict' object is, since I haven't specified that anywhere.

like image 663
Nirupama Rachuri Avatar asked Sep 07 '25 22:09

Nirupama Rachuri


1 Answers

I think you mix up the boto3 client send_mesasge Boto3 client send_message with the boto3.resource.sqs ability.

First, for boto3.client.sqs.send_message, you need to specify QueueUrl. Secondly, the the error message appear because you write incorrect print statement.

# print() function think anything follow by the "queue" are some dictionary attributes  
print queue responses = queue.send_message(MessageBody='Test')

In addition, I don't need to use boto3.session unless I need to explicitly define alternate profile or access other than setup inside aws credential files.

import boto3 
sqs = boto3.client('sqs') 
queue = sqs.get_queue_url(QueueName='queue_name')
# get_queue_url will return a dict e.g.
# {'QueueUrl':'......'}
# You cannot mix dict and string in print. Use the handy string formatter
# will fix the problem   
print "Queue info : {}".format(queue)

responses = sqs.send_message(QueueUrl= queue['QueueUrl'], MessageBody='Test')
# send_message() response will return dictionary  
print "Message send response : {} ".format(response) 
like image 188
mootmoot Avatar answered Sep 11 '25 08:09

mootmoot