简体   繁体   中英

Javascript || AWS S3 SDK & croppie file upload errors

I am trying to upload the cropped results of croppie to an S3 bucket. 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.

I believe the error has something to do with how croppie is packaging the cropped results. I have included my app.js file (where I handle the upload) and the code where the addPhoto function is being called. Resp is the response from croppie.

The expected outcome is that I can successfully crop a photo and then upload it to my S3 bucket.

            $('.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);
    }
  );
}

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.

<AllowedHeader>*</AllowedHeader>

Useful resources: Upload missing ETag , Uploading base64 image to Amazon with Node.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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