从getSignedUrl返回的URL不起作用

时间:2018-09-20 00:51:30

标签: node.js aws-sdk aws-sdk-js

我第一次在Node.js上使用aws-sdk实现aws s3

我目前正在尝试从客户端获取并签名。但是,当我尝试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
  }
});

当我到达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.

我更改了创建的新凭据并进行了设置,但是仍然没有进展。

关于我可能做错了什么的任何线索?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

问题是预检失败,涉及我的存储桶的旧CORS配置。通过添加以下配置对其进行了修复:

<CORSRule>
  <AllowedOrigin>*</AllowedOrigin>
  <AllowedMethod>GET</AllowedMethod>
</CORSRule>
<CORSRule>
  <AllowedOrigin>*</AllowedOrigin>
  <AllowedMethod>PUT</AllowedMethod>
</CORSRule>