我正在尝试通过在 node.js 中创建签名 url,使用 React Native 中的 axios 将图像插入到 digital ocean 空间中。 url 已正确生成,但是当我尝试使用 url 插入图像时,出现“签名不匹配”错误。这是我的 axios 代码:
const fd = new FormData();
fd.append("image", {
uri: this.state.image.path,
type: "image/jpeg",
name: data.key
});
axios(data.url, {
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
},
method: 'PUT',
data: fd
}).then((data) => {
console.log(data);
});
这是我的 node.js 代码:
const spacesEndpoint = new AWS.Endpoint("xyz");
const s3 = new AWS.S3({
endpoint: spacesEndpoint,
accessKeyId: "xyz",
secretAccessKey: "yzx"
});
app.post("/getPreSignedURL", function(req, res) {
const params = {
Bucket: "trialspace",
Key: Math.random() + ".jpg",
ContentType: 'multipart/form-data'
};
s3.getSignedUrl("putObject", params, function(err, url) {
console.log("Your generated pre-signed URL is", url);
var contentType = params.ContentType;
var key = params.Key;
res.send({ url, contentType, key });
});
});
但是,当我使用 xmlHttpRequest 时,如图所示 here而不是 axios 一切正常。所以我不认为空间权限或节点服务器有什么问题。
编辑: 这是来自 axios 的请求和响应日志。由于某种原因,axios 正在从 header 中删除我的内容类型。
最佳答案
我通过删除转换请求中的授权 header 解决了这个问题,如下所示:
axios({
url: "mySignedUrl",
data: {
uri: this.state.image.path,
type: "image/jpeg",
name: "0.3789520076534978.jpg"
},
method: "put",
headers: {
"Content-Type": "image/jpeg",
"x-amz-acl": "public-read"
},
transformRequest: [
(data, headers) => {
delete headers.common.Authorization;
return data;
}
]
})
此问题正在讨论 here
关于javascript - 使用 axios 使用签名链接将图像放入 Digital Ocean 空间时出现 "SignatureDoesNotMatch",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48597477/