从 getSignedUrl 返回 URL 无效

Returned URL from getSignedUrl not working

我第一次使用 aws-sdk 在 Node.js

上实施 aws s3

我目前正在尝试从客户端获取 SignedUrl 并 PUT 到它。但是,当我尝试 PUT 时,它将 return 一个 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.

我更改了创建的新凭据并设置了它们,但仍然没有进展..

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

提前致谢!

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

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