繁体   English   中英

AWS SQS boto3 send_message 返回“dict”对象没有属性“send_message”

[英]AWS SQS boto3 send_message returns 'dict' object has no attribute 'send_message'

  • 我是 python 的新手,我仍在学习它。
  • 我正在编写一个 python 代码来向 AWS SQS 发送消息
  • 以下是我写的代码

    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 行,在 response = queue.send_message(MessageBody='Test')

    AttributeError: 'dict' 对象没有属性 'send_message'

  • 我不确定“dict”对象是什么,因为我没有在任何地方指定。

我认为您将 boto3 客户端 send_mesasge Boto3 客户端 send_message与 boto3.resource.sqs 功能混淆了。

首先,对于boto3.client.sqs.send_message,需要指定QueueUrl。 其次,出现错误信息是因为你写了不正确的打印语句。

# 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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM