How do I handle CORS with html2Canvas and AWS S3 images?

前端 未结 2 1737
时光取名叫无心
时光取名叫无心 2021-02-05 22:27

I know similar questions have been asked before but I still can\'t make it work. I have a div with images inside of it loaded from a bucket in AWS s3, they load per

相关标签:
2条回答
  • 2021-02-05 22:55

    I solved this error with adding crossOrigin attribute at IMG tag. So, your code will look like (tag at React js):

    <Image crossOrigin="true" />
    

    And my configuration at S3 bucket with CORS:

    <?xml version="1.0" encoding="UTF-8"?>
    <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>http://localhost:8000</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Authorization</AllowedHeader>
    </CORSRule>
    <CORSRule>
        <AllowedOrigin>https://testing.d1wr8lk28mi6l0.amplifyapp.com</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Authorization</AllowedHeader>
    </CORSRule>
    </CORSConfiguration>
    

    HTML2CANVAS:

     html2canvas(getWrapper, { allowTaint: true, useCORS: true, logging: true })
      .then((canvas) => {
        const imgData = canvas.toDataURL('image/png');
        console.log(imgData);
      });
    
    0 讨论(0)
  • 2021-02-05 23:02

    See the edit, I did try setting the crossOrigin attribute with no luck, and I use useCORS set to true (forgot to mention that sorry). Still no luck.

    I fixed some cors issues I was having with the combination of Google Chrome, AWS S3, and multiple origins.

    I found this stackoverflow thread: Chrome + CORS + cache - requesting same file from two different origins

    Which links to this bug report: https://bugs.chromium.org/p/chromium/issues/detail?id=260239

    Anyhow as workaround solution you can try this modified version of html2canvas: https://gist.github.com/CrandellWS/6bc2078aced496004d7a045e6360f19b

    use the options:

    allowTaint : false,
    useCORS: true
    

    Hope that helps.

    FYI, this will add the current time stamp to cors image urls to sidestep a cache issue I was having on Chrome... https://gist.github.com/CrandellWS/6bc2078aced496004d7a045e6360f19b#file-html2canvas-js-L6838

    Which means it will effect performance by re-downloading those images...

    original post: https://github.com/niklasvh/html2canvas/issues/1544#issuecomment-435640901

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