upload canvas data to s3

前端 未结 5 1545
刺人心
刺人心 2020-12-25 13:53

Now since the amazon has enabled CORS I was wondering if this is possible.

Can the html canvas data (on client browser) be converted to a <

相关标签:
5条回答
  • 2020-12-25 14:32

    There is an old post method to upload data from browser to s3

    http://s3.amazonaws.com/doc/s3-example-code/post/post_sample.html

    then I have used this idea Convert Data URI to File then append to FormData

    and instead of normal POST there can be an xhr request with the formdata to amazon and you are done

    0 讨论(0)
  • 2020-12-25 14:33

    Using toBlob:

    canvas.toBlob((blob) => {
      if (blob === null) return;
      bucket.upload({
        Key: "where/the/file/goes.jpg"
        ContentType: "image/jpeg",
        Body: blob,
      }, (err, data) => {});
    }, "image/jpeg");
    
    0 讨论(0)
  • 2020-12-25 14:35

    The easiest way to save canvas is to convert it to base64:

    canvas.toDataURL();
    

    or you can set image type via argument:

    canvas.toDataURL("image/png");
    canvas.toDataURL("image/jpeg");
    // etc
    

    Also watch this lib: http://www.nihilogic.dk/labs/canvas2image/

    0 讨论(0)
  • 2020-12-25 14:43

    Here is a working example where you take a data URL from a canvas and upload it to S3:

    var dataUrl = canvas.toDataURL("image/jpeg");
    var blobData = dataURItoBlob(dataUrl);
    var params = {Key: "file_name", ContentType: "image/jpeg", Body: blobData};
    bucket.upload(params, function (err, data) {});
    

    dataURItoBlob:

    function dataURItoBlob(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/jpeg'});
    }
    
    0 讨论(0)
  • 2020-12-25 14:47

    I was researching this and not having much luck until I found this post: https://github.com/aws/aws-sdk-js/issues/1712#issuecomment-329542614

    AWS has a utility that will decode base64 in their aws-sdk: AWS.util.base64.decode(image)

    Simple solution, and worked for me.

    0 讨论(0)
提交回复
热议问题