簡體   English   中英

無法使用node.js和angular將圖像Blob上傳到AWS S3

[英]Unable to upload image blob to AWS S3 using node.js & angular

我無法將圖像上傳到AWS S3

{[[InvalidParameterType:期望的params.body是字符串,緩沖區,流,Blob或類型的數組對象]消息:'期望的params.body是一個字符串,Buffer,Stream,Blob或類型化的數組對象,” 'InvalidParameterType',

背景信息因此,這就是我想要做的事情:首先,我使用Angular Image Cropper( http://andyshora.com/angular-image-cropper.html )將圖像裁剪為可接受的移動尺寸。 這給出了base64 URI。

然后,我使用dataURItoBlob將URI轉換為blob,然后嘗試上傳到AWS S3。

代碼如下:

$scope.dataURItoBlob= function(dataURI) {
    var binary = atob(dataURI.split(',')[1]);
    var array = [];
    for(var i = 0; i < binary.length; i++) {
        array.push(binary.charCodeAt(i));
    }
    return new Blob([new Uint8Array(array)], {type: 'image/png'});
}


$scope.uploadPic = function() {
    var base64data = document.getElementById('base64').value;

    console.log("the data is: "+ base64data);
    $scope.pic.Body = $scope.dataURItoBlob(base64data);
    console.log("the blob is: "+ $scope.pic.Body);

    $http({
    method: 'POST',
    url: '/api/upload/picture',
    data: $scope.pic
  }).success(function (data) {
    //success code
  });

};  

節點(后端)

exports.uploadPicture = function (req, res) {
        if (!req.body.hasOwnProperty('Key') || !req.body.hasOwnProperty('Body')) {
            res.statusCode = 400;
            return res.send('Error 400: Post syntax incorrect.');
        } else {
            var key = req.body.Key;
            var body = req.body.Body;

            AWS.config.loadFromPath('./config.json');

            var s3 = new AWS.S3(); 
            s3.createBucket({Bucket: 'bucket'}, function() {
              var params = {Bucket: 'bucket', Key: key, Body: body};
              s3.putObject(params, function(err, data) {
              if (err) {
              console.log(err);
                res.status(400).send('Bad Request.');
              } else {
                console.log("Successfully uploaded data to myBucket/myKey");   
              }
            });
            });
        }

}

它失敗,因為它期待一個Blob,但是它拒絕了我發送的Blob。請幫助!

謝謝!

您的“ var base64data”里面有什么?

嘗試這個:

 buf = new Buffer(req.body.imageBinary.replace(/^data:image\/\w+;base64,/, ""),'base64')
  var data = {
    Key: req.body.userId, 
    Body: buf,
    ContentEncoding: 'base64',
    ContentType: 'image/jpeg'
  };
  s3Bucket.putObject(data, function(err, data){
      if (err) { 
        console.log(err);
        console.log('Error uploading data: ', data); 
      } else {
        console.log('succesfully uploaded the image!');
      }
  });

希望能有所幫助

暫無
暫無

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

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