2

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!

2
  • What are the types for key and body? What happens when you insert console.dir(key); and console.dir(body); right after you set those two variables? Commented Sep 23, 2014 at 12:59
  • hi mscdex, for key it shows: "filename.png", body it shows: "[object Blob]" as expected. I am able to upload if i change blob to garbage text, but of course, what I want is an image. Commented Sep 23, 2014 at 13:11

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.