【问题标题】:getting 400 Bad Request when trying to upload to aws s3 bucket尝试上传到 aws s3 存储桶时收到 400 Bad Request
【发布时间】:2017-11-06 21:55:51
【问题描述】:

我在我的服务器上签署 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%2BFzWUoeFD02RF2CQ2o0bLmo%3D&x-amz-acl=public-read

然后,当我在客户端上获取该 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) Bad Request

【问题讨论】:

    标签: javascript node.js reactjs amazon-web-services amazon-s3


    【解决方案1】:

    猜你提供的标题错误

    为我工作

     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);
    }
    

    【讨论】:

    • 我试图用 axios 来做。以上是我以前的做法,所以我再次使用它,哈哈
    【解决方案2】:

    我遇到了同样的问题,在搜索了几个小时后,我能够通过将存储桶的区域添加到服务器端后端来解决它,在该后端我使用 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
        }
      });
    

    希望这能解决您的问题。

    【讨论】:

    • 我遇到了同样的问题,但是在 AWS.S3 对象中添加了区域属性和 Bucket 的正确区域后,我救了我的命 :) 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多