Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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!

share|improve this question
    
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? – mscdex Sep 23 '14 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. – ssxdots Sep 23 '14 at 13:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.