2016-12-01 46 views
3

我試圖通過,然後檢索帶有AWS SQS屬性的消息。 儘管我可以通過管理控制檯查看消息的屬性,但我無法使用boto3獲取它們,始終得到None。改變「AttributeNames」並沒有什麼不同。郵件正文可以檢索OK。使用boto3無法訪問SQS消息屬性

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 

回答

5

屬性(生成系統)和消息屬性(用戶定義)是兩個不同的種由back-end API提供的東西。

您正在查找消息屬性,但您要求代碼提取屬性。

AttributeNames=['All']` 

好像你必須獲取消息屬性...

MessageAttributeNames=['All'] 

...叫queue.receive_messages()時。或者,可以使用您想要的特定消息屬性名稱(如果已知),而不是使用'All'(我設計了此API - 本來稱爲'*',但我離題了)。

不可否認,這只是基於對基礎API的熟悉而形成的一種直覺猜測,但它似乎與http://boto3.readthedocs.io/en/latest/guide/sqs.html一致。

+1

它的工作,你猜對了))謝謝 –