以下是我写的代码
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':'@@'}}
追踪(最近一次通话): 文件“publisher_dropbox.py”,第77行,in responses = queue.send_message(MessageBody ='Test')
AttributeError:'dict'对象没有属性'send_message'
我不确定'dict'对象是什么,因为我没有在任何地方指定。
答案 0 :(得分:1)
我认为你将boto3客户端send_mesasge Boto3 client send_message与boto3.resource.sqs能力混淆。
首先,对于boto3.client.sqs.send_message,您需要指定QueueUrl。其次,出现错误消息是因为您编写了错误的print语句。
# print() function think anything follow by the "queue" are some dictionary attributes
print queue responses = queue.send_message(MessageBody='Test')
此外,我不需要使用boto3.session,除非我需要在aws凭证文件中明确定义备用配置文件或访问权限。
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)