I'm trying to upload a image to AWS with Ionic but the file stored in AWS have a error and I can't see the image. The error says: The image ... cannot be displayed because it contains errors. This is the code:
var options = {
quality: 80,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.CAMERA,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: true,
correctOrientation:true
};
$cordovaCamera.getPicture(options)
.then(function(imageData) {
fileName = "image/image"+$scope.itemsListID;
function uploadS3(image, name) {
AWS.config.update({ accessKeyId: "...key...", secretAccessKey: "...key2..." });
AWS.config.region = 'us-east-1';
var bucket = new AWS.S3({params: {Bucket: 'bucketname'}});
var params = {Key: name, ContentType: 'image/jpg', Body: image};
bucket.upload(params, function(err, data){
if(err){
//alert("err");
$ionicPopup.alert({
title: "Server Error",
content: "Failed to upload the image.",
okType: "button-stable"
});
$ionicLoading.hide();
}
else{
//alert("data");
$ionicPopup.alert({
title: "Successful",
content: "Image have been uploaded successfuly.",
okType: "button-stable"
});
$ionicLoading.hide();
}
});
}
uploadS3(imageData, fileName);
}, function(err) {
$ionicPopup.alert({
title: "Server Error",
content: "Failed to connect with the server.",
okType: "button-stable"
});
console.log(err);
});
here is my code. first I converted imageURL to FILE type that can get from Camera or Photolibrary. then I tried uploading an image to AWS s3 bucket.
};
}
}
}
If you have any question, please let me know
I ended up creating a BLOB from the base64 encoded image returned from getPicture.