Amazon SQS FIFO 队列发送消息失败

Amazon SQS FIFO Queue sending message failed

我们正在尝试向 AWS FIFO 队列发送消息。我们已经有了将消息发送到 SQS 标准队列的代码的工作版本。

Python代码(我们要求不使用SDK):Examples of the Complete Version 4 Signing Process (Python)

对于标准队列,我们​​使用了以下参数

    method = 'GET'
    service = 'sqs'
    host = 'sqs.us-west-2.amazonaws.com'
    region = 'us-west-2'
    endpoint = 'https://sqs.us-west-2.amazonaws.com/xxxxxx/TestQueue'
    request_parameters = 'Action=SendMessage&MessageBody=mytest&Version=2012-11-05'
    canonical_uri = '/xxxxxx/TestQueue'

对于FIFO Queue,我们使用相同的代码,另外修改如下

method = 'GET'
service = 'sqs'
host = 'sqs.us-west-2.amazonaws.com'
region = 'us-west-2'
endpoint = 'https://sqs.us-west-2.amazonaws.com/xxxxxxx/Test.fifo'
request_parameters = 'Action=SendMessage&MessageBody=mytest&MessageGroupId=test&MessageDeduplicationId=ttte&Version=2012-11-05'
canonical_uri = '/xxxxxxx/Test.fifo'

但是失败了。我们是否遗漏了什么,有人可以帮助我们吗?

Response code: 403

<?xml version="1.0"?><ErrorResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/"><Error><Type>Sender</Type><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

签名算法要求您在签名前按词法对参数进行排序。这就是为什么使用术语 canonical 来描述 "canonical query string." 它们不一定需要在实际请求中发送到服务器、排序,但它们必须对签名进行排序才能产生正确的结果。

MessageGroupId 需要在 MessageDeduplicationId 之后,而不是在

之前

您链接到的页面上的代码示例中提到了这一点:

# Step 3: Create the canonical query string. In this example (a GET request),
# request parameters are in the query string. Query string values must
# be URL-encoded (space=%20). The parameters must be sorted by name.
# For this example, the query string is pre-formatted in the request_parameters variable.

比这个简化示例更好的实现可能将参数作为字典,并对它进行排序,以构建规范的查询字符串。更好的实现还可以处理 url-自动转义键和值。