我正在尝试将croppie的裁剪结果上传到S3存储桶中。当我成功裁剪并尝试上传裁剪的结果时,我当前遇到一个空白错误。
我遵循了亚马逊的文档,包括设置S3存储桶、身份池和配置我的CORS。
我相信这个错误与croppie如何包装裁剪后的结果有关。我已经包含了我的app.js文件(我在其中处理上传(和调用addPhoto函数的代码。Resp是croppie的回应。
预期的结果是,我可以成功地裁剪一张照片,然后将其上传到我的S3存储桶中。
$('.crop').on('click', function (ev) {
$uploadCrop.croppie('result', {
type: 'canvas',
size: 'original'
}).then(function (resp) {
Swal.fire({
imageUrl: resp,
showCancelButton: true,
confirmButtonText: "Upload",
reverseButtons: true,
showCloseButton: true
}).then((result) => {
if(result.value) {
addPhoto(resp);
}
app.js
var albumBucketName = "colorsort";
var bucketRegion = "xxx";
var IdentityPoolId = "xxx";
AWS.config.update({
region: bucketRegion,
credentials: new AWS.CognitoIdentityCredentials({
IdentityPoolId: IdentityPoolId
})
});
var s3 = new AWS.S3({
apiVersion: "2006-03-01",
params: { Bucket: albumBucketName }
});
function addPhoto(resp) {
var file = resp;
var fileName = file.name;
console.log(resp.type);
var photoKey = fileName;
// Use S3 ManagedUpload class as it supports multipart uploads
var upload = new AWS.S3.ManagedUpload({
params: {
Bucket: albumBucketName,
Key: photoKey,
Body: file,
ACL: "public-read"
}
});
var promise = upload.promise();
promise.then(
function(data) {
alert("Successfully uploaded photo.");
},
function(err) {
return alert("There was an error uploading your photo: ", err.message);
}
);
}
我发现的解决方案包括将以下代码段添加到我的CORS配置中,以及将croppie结果"type:"从canvas更改为base64。
<AllowedHeader>*</AllowedHeader>
有用的资源:上传丢失的ETag,使用Node.js 将base64图像上传到亚马逊