簡體   English   中英

嘗試上傳到AWS S3存儲桶時收到400錯誤請求

[英]getting 400 Bad Request when trying to upload to aws s3 bucket

我在服務器上簽署了URL,然后將其發送回客戶端,效果很好。 這就是該功能的外觀

const aws = require('aws-sdk'),
    config = require('config'),
    crypto = require('crypto');


module.exports = async function(file_type) {

    aws.config.update({accessKeyId: config.AWS_ACCESS_KEY, secretAccessKey: config.AWS_SECRET_KEY})

    const s3 = new aws.S3();

    try {
        if (!file_type === "image/png") {
            return ({success: false, error: 'Please provide a valid video format'});
        }
        let buffer = await crypto.randomBytes(12);

        let key = buffer.toString('hex');

        let options = {
            Bucket: config.AWS_S3_BUCKET,
            Key: key,
            Expires: 60,
            ContentType: file_type,
            ACL: 'public-read',
        }

        let data = await s3.getSignedUrl('putObject', options);
        console.log('data was', data)
        return ({
            success: true,
            signed_request: data,
            url: ('https://s3.amazonaws.com/' + config.AWS_S3_BUCKET + '/' + key),
            key,
        });
    } catch (error) {
        console.log('the error was', error)
        return ({
            success: false,
            error: error.message,
        })
    }
}

所以這很好用,最終得到一個像

https://mybucket.s3.amazonaws.com/a33b4a43f23fc41de9ddck1k?AWSAccessKeyId=ADIFJDGPMRFRGLXSYWPQ&Content-Type=image%2Fpng&Expires=1496716543&Signature=0zcx%2BFzWUoeFD02RF2CQ2oxb-am%

然后,當我在客戶端上獲得該URL時。.我使用axios發送具有以下功能的PUT請求-

function uploadToS3(file, signedRequest, callback){

    var options = {
        headers: {
            'Content-Type': file.type
        }
    };

    axios.put(signedRequest, file, options)
        .then(result =>{
            console.log('the result was', result)
            callback(result)
        })
        .catch(err =>{
            callback(err)
        })

}

我唯一回來的是(400)錯誤的請求

我遇到了同樣的問題,經過幾個小時的搜索,我能夠通過將存儲桶的區域添加到服務器端后端使用s3.getSignedUrl()請求簽名的URL)來解決此問題

const s3 = new AWS.S3({
    accessKeyId:"your accessKeyId",
    secretAccessKey:"your secret access key",
    region:"ap-south-1" // could be different in your case
})
const key = `${req.user.id}/${uuid()}.jpeg`

s3.getSignedUrl('putObject',{
        Bucket:'your bucket name',
        ContentType:'image/jpeg',
        Key:key
    }, (e,url)=>{
        res.send({key,url})
})

獲取簽名的URL后,我在客戶端使用axios.put()使用URL將圖像上傳到我的s3存儲桶。

  const uploadConf = await axios.get('/api/uploadFile');
  await axios.put(uploadConf.data.url,file, {
    headers:{
      'Content-Type': file.type
    }
  });

希望這能解決您的問題。

猜猜您提供的標題錯誤

為我工作

 function upload(file, signedRequest, done) {
  const xhr = new XMLHttpRequest();
  xhr.open('PUT', signedRequest);
  xhr.setRequestHeader('x-amz-acl', 'public-read');
  xhr.onload = () => {
    if (xhr.status === 200) {
      done();
    }
  };

  xhr.send(file);
}

暫無
暫無

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

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