简体   繁体   English

如何在 AWS 上读取 sqs 消息的正文

[英]How to read body of the sqs message on AWS

I am writing a script in python to receive Amazon SQS messages.我正在 python 中编写一个脚本来接收 Amazon SQS 消息。

In the Amazon SQS Queue I have such message as在 Amazon SQS 队列中,我有这样的消息

'{"Test":"Machine-name", "State":"Request", "Description":""}'

In the python script I can receive this message;在 python 脚本中我可以收到这条消息; however, it complains as following:但是,它抱怨如下:

msg = json.loads(message)
  File "/usr/lib/python3.8/json/__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not sqs.Message

I believe the message I am inserting to the queue is in json format, but why does it recognize as sqs.Message?我相信我插入队列的消息是 json 格式,但为什么它识别为 sqs.Message?

How do I convert to json format?如何转换为 json 格式?

Thanks,谢谢,

When you read an SQS message (or rather a list of SQS messages) via Boto3 (the Python AWS SDK) you are not given the payload or body of a single message directly, but the dict containing a list of messages where each message includes other attributes like MessageId.当您通过 Boto3(Python AWS SDK)读取 SQS 消息(或者更确切地说是 SQS 消息列表)时,您不会直接获得单个消息的有效负载正文,而是包含消息列表的字典,其中每条消息都包含其他消息MessageId 等属性。

As part of a message (in the list of messages) you have the Body attribute, which is what you want.作为消息的一部分(在消息列表中),您具有Body属性,这正是您想要的。

See: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.receive_message请参阅: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.receive_message

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

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