Python boto3 和 SQS 没有收到消息

Python boto3 and SQS not receiving messages

我正尝试在 Python 中使用 Amazon 的 SQS 实现发送和接收消息 我没有做任何事情让我的代码的接收端检索我发送的消息, 我做错了什么?

send.py:

SESSIONS_ACCESS_KEY = "************" 
SESSIONS_SECRET_KEY = "************"
sess = boto3.session.Session(aws_access_key_id=SESSIONS_ACCESS_KEY, aws_secret_access_key=SESSIONS_SECRET_KEY, region_name='eu-central-1')
sqs = sess.resource("sqs")
queue = sqs.get_queue_by_name(QueueName='myTestQueue.fifo')
response = queue.send_message(
    MessageBody="TEST 123",
    MessageGroupId='messageGroup1'
)

print(response.get('MessageId'))
print(response.get('MD5OfMessageBody'))

rec.py:

import time
import boto3
   
SESSIONS_ACCESS_KEY = "************" 
SESSIONS_SECRET_KEY = "************"
sess = boto3.session.Session(aws_access_key_id=SESSIONS_ACCESS_KEY, aws_secret_access_key=SESSIONS_SECRET_KEY, region_name='eu-central-1')
sqs = sess.resource("sqs")

queue = sqs.get_queue_by_name(QueueName='myTestQueue.fifo')

# Process messages by printing out body
while True:
    messages = queue.receive_messages()
    print('Amount of existing Queue messages',len(messages))
    for message in messages:
        print('msg:',message.body)
        message.delete()
    time.sleep(5)

当 运行 send.py python 文件时,我得到以下内容:

1451fdb5-0f95-45d6-b1c1-76d9092fb49a
911e12e2292eb0914f39540ae513721c

但是 rec.py python 文件我一直在队列通知中收到 0 条消息:

Amount of existing Queue messages 0
Amount of existing Queue messages 0
Amount of existing Queue messages 0
Amount of existing Queue messages 0

我做错了什么?我也应该在接收端设置 MessageGroupId 吗?我应该使用其他命令来发送或接收消息吗?

当我运行你的send.py代码时,我收到一条错误信息:

botocore.exceptions.ClientError: An error occurred (InvalidParameterValue) when calling the SendMessage operation: The queue should either have ContentBasedDeduplication enabled or MessageDeduplicationId provided explicitly

这是 FIFO 队列的功能,可避免重复消息。我不确定你在队列中配置了什么,所以我激活了 ContentBasedDeduplication 然后收到了一条类似于你显示的日志消息。

然后我能够重现您未收到消息的情况。

但是,鉴于启用了ContentBasedDeduplication,我随后修改了send.py

response = queue.send_message(
    MessageBody="TEST 123"+ str(random.random()),
    MessageGroupId='messageGroup1'
)

使用此代码发送消息时,消息已被 rec.py 成功接收

底线:FIFO 队列正在过滤重复消息,这可能是您配置它的方式。