在 URL 中使用冒号 (:) 请求 AWS 时签名无效

Invalid signature when requesting AWS with colon (:) in the URL

我正在向 AWS 发出请求:POST https://myapi.com/users/us-west-2:123

如果我删除 us-west-2: 它工作正常但包括生成

The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method.

这似乎是 : 的原因,因为它需要 URL 编码。使用编码的 uri 生成签名会产生相同的错误!

POST https://myapi.com/users/us-west-2%3A123

这是怎么回事?我的签名生成器使用

{
  "path": "/users/us-west-2%3A123",
  "headers": {
    "X-Amz-Date": timestamp,
    "host": "myapi.com",
  },
  "body": "",
}

我用这个生成器向 https://myapi.com/users/us-west-2%3A123 发出一个没有正文的 POST 请求。

哇。我知道了。

这是正在发生的事情:

我正在生成请求 /users/us-west-2%3A123,当我生成签名时,我使用的包 (react-native-aws-signature) 将 % 编码为百分比,因此它将 % 变成了 %25

解决方法是切换到 aws4 或者更确切地说是分支 aws4-react-native 并在签名请求中使用 doNotEncodePath: true 选项。我也在向 /users/us-west-2:123.

发出 fetch 请求(节点)

我要感谢亚马逊支持,GitHub,还有运气。

我也遇到了这个问题。在我们的应用程序中,我们希望能够像这样将 id 作为路径参数发送

/admin/eu-west-1:xxxx-xxxx-xxxx-xxx
error Msg: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method.

如果我删除冒号 (:) 并将其替换为任何其他字符,例如 % 或任何 number/char,我的请求就会生效。

我知道有一些可能的解决方案可以重构 API 以接受 2 个单独的参数,例如区域和 ID。

/users/{region}/{id}
Example: /users/eu-west-1/xxxx-xxxx-xxxx-xxx

但是因为我们想为我们的 API 遵循严格的模式,所以这对我们来说不是最好的解决方案。

我们的模式:

/someObject
GET - get a list
POST - Create
PUT - Update ALL
/{id}
    GET - Get one
    PUT - Update one
    Delete - Delete one