I have cordova app where the user must be able to select an image off their phone and upload it to AWS S3. I'm using cordova-plugin-camera and the S3 SDK for this. I have code that works fine (see below), but requires the entire image to be pulled into memory inside a base64 encoded string (per Camera.DestinationType.DATA_URL) and then again into a Buffer.
I'd prefer to have this streamed off the phone's hard drive to mitigate potential memory issues. The cordova-plugin-camera website even has a warning about this:
/** * Warning: Using DATA_URL is not recommended! The DATA_URL destination * type is very memory intensive, even with a low quality setting. Using it * can result in out of memory errors and application crashes. Use FILE_URI * or NATIVE_URI instead. */
So is there any way to stream the photo straight from the disk up to S3 using FILE_URI or NATIVE_URI or any other technique?
Here's my current code:
doTheThing() {
let options = {
quality: 50,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
encodingType: Camera.EncodingType.JPG
};
navigator.camera.getPicture(imgData => {
this.uploadToS3(imgData);
}, options);
uploadToS3(data) {
let AWS = require('aws-sdk');
AWS.config = new AWS.Config();
AWS.config.accessKeyId = 'noway';
AWS.config.secretAccessKey = 'jose';
AWS.config.region = 'us-east-2';
let s3 = new AWS.S3();
let uploadParams = { Bucket: 'iamkule/f1/f2', Key: '', Body: new Buffer(data, 'base64') };
uploadParams.Key = 'myfile.jpg';
s3.upload(uploadParams, function (err, data) {
if (data) {
console.log('yay!');
}
});
}
}