无法使用boto3访问SQS消息属性

13

我正在尝试通过AWS SQS传递和检索带有属性的消息。虽然我可以通过管理控制台看到消息的属性,但使用boto3无法获取它们,始终返回None。更改“AttributeNames”没有任何影响。消息正文可以成功检索。

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
1个回答

21

属性(系统生成)和消息属性(用户定义)是由后端API提供的两种不同的内容。

您正在寻找消息属性,但您要求代码获取属性。

AttributeNames=['All']`

看起来你需要获取消息属性...

MessageAttributeNames=['All']

当调用queue.receive_messages()时,您可以使用特定的消息属性名称(如果已知),而不是'All' (如果我设计了这个API,它将被称为'*',但我偏离了主题)。

诚然,这只是我根据对底层API的熟悉程度做出的直觉猜测,但它似乎与http://boto3.readthedocs.io/en/latest/guide/sqs.html相一致。


5
为什么我讨厌 AWS API 的又一个原因。 - confiq

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接