NodeJS 我们计算的请求签名与您提供的签名不匹配,检查您的Google密钥和签名方法

l3zydbqr  于 2023-06-05  发布在  Node.js
关注(0)|答案(2)|浏览(157)

我试图得到一个签名的网址,然后上传一个文件,但它返回一个错误,我还没有能够解决,我已经看到其他问题,但什么都没有,我试图与一个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))
        }
      })
qcbq4gxm

qcbq4gxm1#

您在Postman上得到错误,因为您使用GET发送请求。将请求方法更改为PUT
在您的代码中,问题的根本原因仅仅是因为打字错误。如果你检查文档,正确的配置属性应该是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)
  }
})

有关其他参考,请参见www.example.com中的Content-Typehttps://cloud.google.com/storage/docs/access-control/signed-urls-v2#string-components

bvuwiixz

bvuwiixz2#

我不认为这是对你问题的回答,但对谁来说可能有同样的问题。当我尝试部署云功能时,我遇到了同样的问题。然后我意识到VPN导致了这一点。关闭vpn修复了它。

相关问题