我试图得到一些图像到S3,但不是很成功......这是我的工作至今
$cordovaCamera.getPicture(options).then(function(imageURI) {
// imageURI will be something like: file:///some_path
// get the base64 data from the image
var img = Utils.encodeImageUri(imageURI);
// get the base64 from a new image, not sure if this is needed
var image = new Image();
image.src = img;
Utils.uploadToS3(image.src);
}, function(err) {})
...
// boilerplate function to create a Blob
dataURItoBlob: function(dataURI) {
var binary = atob(dataURI.split(',')[1]);
var array = [];
for (var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
return new Blob([new Uint8Array(array)], {
type: mimeString
});
},
// the actual upload
uploadToS3: function(imageData) {
var imgData = this.getImgData(imageData); // will return Object {extension: "jpg", type: "image/jpeg"}
var data = this.dataURItoBlob(imageData);
AWS.config.update({accessKeyId: accessKey, secretAccessKey: secretKey});
var bucket = new AWS.S3({params:{Bucket: 'testbucket'}});
var params = {
Key: 'main.' + imgData.extension,
Body: data,
ContentEncoding: 'base64',
ContentType: imgData.type
};
bucket.upload(params, function(err, data) {
console.log(data.Location); // this will be the path to my image on s3
});
}
所有我做的是刚刚抓住base64
从图像数据,生成一个Blob
,并把它发送到亚马逊
我得到的桶中的形象,所以在发送作品数据,但图像是黑色或有时打破
有任何想法吗?