【问题标题】:Returned URL from getSignedUrl not working从 getSignedUrl 返回的 URL 不起作用
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-05-10
    • 2014-05-04
    • 2019-04-29
    • 2016-11-15
    • 2014-10-11
    • 2016-09-03
    • 1970-01-01
    • 2021-08-09
    • 2017-08-08
    相关资源
    最近更新 更多