【问题标题】:Trying to Send the message from to Amazon SQS尝试将消息从 Amazon SQS 发送
【发布时间】:2022-01-11 14:27:04
【问题描述】:

我正在使用 python 和 boto sqs 发送和接收消息,现在我想将消息发送到 aws FIFO 队列,但我没有找到任何关于如何使用 boto 设置 MessageGRoupID 的资源,有很多可用于使用 boto3 发送消息的资源,但我想使用 boto 设置消息组 ID,任何人都可以帮忙,

我当前的代码是:

def _send_msg_to_Fifo_queue(queue_name,message_body,message_groupID,message_DeduplicationId):

    q = conn.get_queue(queue_name)

    if not q:
        # TODO idk, this should never happen, but log or something?
        pass
    m=Message()
    m.set_body(message_body)
    m.MessageGroupID=message_groupID
    q.write(m)

【问题讨论】:

  • 当前代码有什么问题?还有你为什么提到需要使用boto,有什么理由不能使用boto3?
  • 我无法使用 Boto 设置 MessageGroupId ,上面的程序设置了一个错误,指出没有名为 MessageGroupId 的属性,我设置了 using 消息属性,虽然它说缺失并且因为有多个地方我们需要安装 boto3 ,因为它在多个服务器上运行。如果有任何方法可以使用 boto 设置 MEssageGroupIF 我不想安装 boto3
  • 使用set_queue_attribute怎么样?它可以让你设置队列的属性。
  • 已尝试,堆栈跟踪:boto.exception.SQSError: SQSError: 400 Bad Request queue.amazonaws.com/doc/2012-11-05/… Attribute MessageGroupId.
  • 好吧,我猜你可能不走运。如果您查看亚马逊关于 FIFO 队列here 的公告,它似乎是在 2016 年才引入的,我猜当时 boto3 已经在这里,所以我现在甚至不确定这是否已被向后移植到 boto( 2) 完全没有!

标签: python boto amazon-sqs


【解决方案1】:

这是一个老问题,但我为前来寻找解决方案的人留下了答案。

SQS 发送消息请求语法:

response = queue.send_message(
    MessageBody='string',
    DelaySeconds=123,
    MessageAttributes={
        'string': {
            'StringValue': 'string',
            'BinaryValue': b'bytes',
            'StringListValues': [
                'string',
            ],
            'BinaryListValues': [
                b'bytes',
            ],
            'DataType': 'string'
        }
    },
    MessageSystemAttributes={
        'string': {
            'StringValue': 'string',
            'BinaryValue': b'bytes',
            'StringListValues': [
                'string',
            ],
            'BinaryListValues': [
                b'bytes',
            ],
            'DataType': 'string'
        }
    },
    MessageDeduplicationId='string',
    MessageGroupId='string'
)

所以你可以像这样修改你的代码:

def _send_msg_to_Fifo_queue(queue_name,message_body,message_groupID,message_DeduplicationId):
    q = boto3.client('sqs').get_queue(queue_name)

    if not q:
        # TODO idk, this should never happen, but log or something?
        pass

    response = q.send_message(
        MessageBody=message_body,
        MessageGroupId=message_groupID,
        MessageDeduplicationId=message_DeduplicationId
    )

boto3 官方文档:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.send_message

【讨论】:

    猜你喜欢
    • 2010-12-12
    • 2023-03-23
    • 1970-01-01
    • 2018-08-29
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    相关资源
    最近更新 更多