簡體   English   中英

從getSignedUrl返回的URL不起作用

[英]Returned URL from getSignedUrl not working

我第一次在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' 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. 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM