【问题标题】:The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method我们计算的请求签名与您提供的签名不匹配。检查您的 Google 密钥和签名方法
【发布时间】:2021-03-09 23:13:11
【问题描述】:

我正在尝试获取签名的 url,然后上传文件,但它返回一个我无法解决的错误,我已经看到其他问题但还没有,我正在尝试使用 png 文件和我在续中指定了它。

 const fileD = storage.bucket(bucket).file(file)
      const config = {
        action: 'write',
        expires: '03-17-2025',
        ContentType: 'image/png'
      }
      fileD.getSignedUrl(config, async function Sing(err, url) {
        if (!err) {
          const options1 = {
            method: 'PUT',
            url,
            headers: {
              'cache-control': 'no-cache',
              'Content-Type': 'image/png'
            },
            data: './uploads/test.png'
          }

          axios(options1)
            .then((response) => res.json(response))
            .catch((error) => res.json(error.response.data))
        }
      })

【问题讨论】:

  • 我认为您计算机上的日期/时间/时区有误。这将导致此错误消息。
  • 我的电脑有今天的日期,如果我请求“读取”图像,它会返回 url,我可以看到图像,但我不能 PUT @JohnHanley
  • 如果日期、时间和时区正确,那么下一个可能性是 HTTP PUT 必须匹配包括标头和对象名称的签名请求。唯一的区别是'cache-control': 'no-cache',。尝试删除它。
  • 我得到同样的错误,如果我删除那行,我也在邮递员中尝试它直接发送到签名的 url,它也不起作用。 @约翰汉利

标签: node.js google-cloud-platform google-cloud-storage


【解决方案1】:

您在 Postman 上收到错误,因为您使用 GET 发送请求。将请求方式改为PUT

在您的代码中,问题的根本原因仅仅是因为拼写错误。如果您检查docs,您的配置的正确属性应该是contentType,而不是ContentType

由于拼写错误,Content-Type 未在 URL 中正确签名,因此在您的请求中添加此标头将导致不匹配错误。

这是您的代码的固定版本:

const fileD = storage.bucket(bucket).file(file)
const config = {
  action: 'write',
  expires: '03-17-2025',
  contentType: 'image/png'
} 
fileD.getSignedUrl(config, async function Sing(err, url) {
  if (!err) {
    const data = fs.readFileSync('./uploads/test.png') 
    const options1 = {
      headers: {
        'Content-Type': 'image/png'
      }
    }
    axios.put(url, data, options1)
      .then((response) => console.log(response.status))
      .catch((error) => console.error(error.response.data))
  }else{
    console.error(err)
  }
})

如需更多参考,请参阅https://cloud.google.com/storage/docs/access-control/signed-urls-v2#string-components 中的Content-Type

【讨论】:

    猜你喜欢
    • 2021-04-11
    • 2020-11-06
    • 2015-06-01
    • 2019-12-01
    • 2015-12-31
    • 2015-05-18
    • 2022-01-05
    • 1970-01-01
    相关资源
    最近更新 更多