Alternative to fs.createReadStream() on Ionic


#1

i have been trying to upload an image from my Ionic App to Amazon S3.

on the official documentation(http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-examples.html#Amazon_Simple_Storage_Service__Amazon_S3_), they have given this as an example:

var fs = require('fs');
var zlib = require('zlib');

var body = fs.createReadStream('bigfile').pipe(zlib.createGzip());
var s3obj = new AWS.S3({params: {Bucket: 'myBucket', Key: 'myKey'}});
s3obj.upload({Body: body}).
  on('httpUploadProgress', function(evt) { console.log(evt); }).
  send(function(err, data) { console.log(err, data) });
///

How can i create a readStream with my Image received from the camera…

var options = {
        quality : 75,
        destinationType : Camera.DestinationType.FILE_URI,
        sourceType : Camera.PictureSourceType.CAMERA,
        //allowEdit : true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 500,
        targetHeight: 500,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false
    };

    $cordovaCamera.getPicture(options).then(function(imageData) {
      // alert(imageData);
        $scope.imgURL=imageData;
}

After i have the imgURL, how can i create an readStream from that?, i can upload the image to my S3 bucket but only few bytes are sent, what’s the problem?


#2

The problem is that the first code-block is nodejs i think and not plain browser javascript.

if you want to upload you need the cordova-file-transfer plugin

A nice tutorial can be found here:
http://coenraets.org/blog/2013/09/how-to-upload-pictures-from-a-phonegap-app-to-amazon-s3/

you should consider the first approach as the simplest one. But please read the others, too ;).


#3

I tried but i am getting an error with code 2, the documentation said that code 2 is for Invalid URL.
Everything i have writtten is fine… do you have any working code of this?