【发布时间】:2018-09-24 01:35:43
【问题描述】:
我第一次在 Node.js 上使用 aws-sdk 实现 aws s3
我目前正在尝试从客户端获取签名Url 和 PUT。但是,当我尝试 PUT 时,它会返回 403 状态码。
这是我的后端代码:
const s3 = new AWS.S3({
accessKeyId: keys.accessKeyId,
secretAccessKey: keys.secretAccessKey
});
app.get('/api/upload', requireLogin, (req, res) => {
let key = `${req.user.id}/${uuid()}.jpeg`
s3.getSignedUrl('putObject', {
Bucket: 'advanced-node-blog',
ContentType: 'image/jpeg',
Key: key
},
(err, url) => res.send({ key, url }));
});
在前端:
// presigned URL
const uploadConfig = await axios.get('/api/upload');
await axios.put(uploadConfig.data.url, file, {
headers: {
'Content-Type': file.type
}
});
当我 GET 到 URL 时,我会收到这条消息:
The authorization mechanism you have provided is not supported. Please use AWS4-HMAC-SHA256.
我研究了这个错误并得出我错过了signatureVersion: 'v4', 的结论。然而,当我添加这个时,错误变为Error parsing the X-Amz-Credential parameter; the region 'us-east-1' is wrong; expecting 'us-east-2'
然后我添加了region: 'us-east-2'。然后错误变为The request signature we calculated does not match the signature you provided. Check your key and signing method.
我更改了创建的新凭据并设置了它们,但仍然没有进展..
关于我可能做错了什么的任何线索?
提前谢谢你!
【问题讨论】:
-
由于您的
key导致的问题 >>>let key =${req.user.id}/${uuid()}.jpeg`` 因为req.user.id在 S3 存储桶中不存在。参考链接:https://github.com/thephpleague/flysystem-aws-s3-v3/issues/121 -
@IftekharDani 我明白我做错了什么......我应该如何解决这个问题?
-
首先您需要在 s3 中创建文件夹,然后您可以使用任何名称存储。在 s3 存储桶上创建文件夹
user。现在你可以存储了:let key =user/${req.user.id}/${uuid()}.jpeg
标签: node.js aws-sdk aws-sdk-js