node.js - 我们计算的请求签名与您提供的签名不匹配。检查您的 Google 密钥和签名方法
问题描述
我正在尝试获取签名的 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))
}
})
解决方案
您在 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)
}
})
有关其他参考,请Content-Type
参阅https://cloud.google.com/storage/docs/access-control/signed-urls-v2#string-components
推荐阅读
- arrays - 在 Forth 上将文本文件写入数组
- image - 在文件之间传输元数据
- redis - 如何检查 Redis 中是否存在消费者组?
- c# - 为什么 BitArray 比布尔数组快?
- c# - 如何为实体框架已跟踪的记录加载相关数据
- c# - Unity 3D,transform.position 不适用于 instatiate 对象
- php - 关于 OOP,使用 PHP
- python - 表 2 中的 Pandas SUMIFS,用于表 1 中的列
- java - 如何在 org.hibernate.annotations.Formula 中使用子表列
- javascript - Mongoose 虚拟定义。箭头函数与“传统”函数