【发布时间】:2018-08-02 23:24:43
【问题描述】:
我正在使用 aws-sdk 获取 S3 的预签名 URL。我将函数包装在 lambda 中。
const aws = require('aws-sdk');
module.exports = CreateRecord => {
CreateRecord.controllers.createSignature = (event, context, callback) => {
const s3 = new aws.S3({
signatureVersion: 'v4',
});
const params = {
Bucket: 'random-test-bucket002',
Key: 'test-key',
Expires: 100
};
s3.getSignedUrl('putObject', params, function(err, signedUrl) {
let response;
if (err) {
response = {
statusCode: 500,
headers: {
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
error: 'Did not receive signed url'
}),
};
} else {
response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
},
body: JSON.stringify({
message: `Url successfully created`,
signedUrl,
})
};
}
callback(null, response);
});
};
};
这段代码工作得很好,我找回了我的预签名网址。当我在前端运行此代码时:
postImage(uuid) {
const getSignature = 'https://xyz.execute-api.us-east-1.amazonaws.com/dev/v1/createSignature';
axios.get(getSignature)
.then(res => {
const signatureUrl = res.data.signedUrl;
// I have a blob that I store in file
// uuid is passed from another function
const file = new File([this.state.cover], uuid);
axios.post(signatureUrl, file)
.then(s3Res => {
console.log(s3Res);
});
});
}
我不断收到的错误是:我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。我尝试弄乱一些内容类型的标头,但没有任何效果。我可以将预签名的 url 传递给 aws-sdk 中的函数吗?我已经查看了很多关于此的帖子,但似乎无法解决问题。
【问题讨论】:
-
尝试 axios.put(...) 以使用 HTTP PUT 而不是 POST。
-
哇...这工作。疯狂的。太感谢了!!!似乎我所寻找的所有地方都在使用 POST
标签: amazon-web-services amazon-s3 aws-sdk pre-signed-url