gpt4 book ai didi

javascript - 无法使用node.js和 Angular 将图像blob上传到AWS S3

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:59 24 4
gpt4 key购买 nike

我无法将图像上传到 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',

背景这就是我想做的:首先,我使用 Angular Image Cropper ( http://andyshora.com/angular-image-cropper.html ) 将图像裁剪为移动设备可接受的大小。这给出了一个base64 URI。

然后,我使用 dataURItoBlob 将 URI 转换为 blob,并尝试上传到 AWS S3。

代码如下所示:

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 (后端)

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");
}
});
});
}

}

它失败了,因为它需要一个 Blob,但它拒绝了我发送的 Blob。请帮忙!

谢谢!

最佳答案

你的“var base64data”里面有什么?

试试这个:

 buf = new Buffer(req.body.imageBinary.replace(/^data:image\/\w+;base64,/, ""),'base64')
var data = {
Key: req.body.userId,
Body: buf,
ContentEncoding: 'base64',
ContentType: 'image/jpeg'
};
s3Bucket.putObject(data, function(err, data){
if (err) {
console.log(err);
console.log('Error uploading data: ', data);
} else {
console.log('succesfully uploaded the image!');
}
});

希望有帮助

关于javascript - 无法使用node.js和 Angular 将图像blob上传到AWS S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25995623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com