简体   繁体   中英

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

I am unable to upload my images to AWS S3

{ [InvalidParameterType: Expected params.Body to be a string, Buffer, Stream, Blob, or typed array object] message: 'Expected params.Body to be a string, Buffer, Stream, Blob, or typed array object', code: 'InvalidParameterType',

Background So this is what I am trying to do: first, I used Angular Image Cropper ( http://andyshora.com/angular-image-cropper.html ) to crop the image to a acceptable size for mobile. This gives a base64 URI.

Then, I used dataURItoBlob to convert the URI to blob, and attempt to upload to AWS S3.

The code looks like this:

Angular

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

};  

Node (backend)

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

}

It fails because it is expecting a Blob but it is rejecting the Blob that I am sending in.. Please help!

Thanks!

what is inside your "var base64data"?

try this:

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

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM