简体   繁体   English

Javascript || AWS S3 SDK &croppie 文件上传错误

[英]Javascript || AWS S3 SDK & croppie file upload errors

I am trying to upload the cropped results of croppie to an S3 bucket.我正在尝试将croppie 的裁剪结果上传到S3 存储桶。 I am currently getting a blank error when I successfully crop and then try to upload the cropped results.当我成功裁剪然后尝试上传裁剪结果时,我目前收到空白错误。

I have followed Amazon docs including setting up the S3 bucket, identity pools, and configuring my CORS.我遵循了 Amazon 文档,包括设置 S3 存储桶、身份池和配置我的 CORS。

I believe the error has something to do with how croppie is packaging the cropped results.我相信这个错误与croppie如何打包裁剪结果有关。 I have included my app.js file (where I handle the upload) and the code where the addPhoto function is being called.我已经包含了我的 app.js 文件(我在其中处理上传)和调用 addPhoto function 的代码。 Resp is the response from croppie. Resp 是来自croppie 的响应。

The expected outcome is that I can successfully crop a photo and then upload it to my S3 bucket.预期的结果是我可以成功裁剪照片,然后将其上传到我的 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应用程序.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);
    }
  );
}

The solution I found involved adding the following snippet to my CORS config as well as changing the croppie result 'type:' from canvas to base64.我发现的解决方案涉及将以下代码段添加到我的 CORS 配置中,以及将作物结果“类型:”从 canvas 更改为 base64。

<AllowedHeader>*</AllowedHeader>

Useful resources: Upload missing ETag , Uploading base64 image to Amazon with Node.js有用的资源:上传缺少的 ETag使用 Node.js 将 base64 图像上传到亚马逊

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM