Reputation: 113
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'))
{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.
Upvotes: 4
Views: 17338
Reputation: 13176
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)
Upvotes: 5