html - Amazon S3 图像,无法使用 html5 Canvas 保存,出现 Tainted Canvases 错误

标签 html canvas amazon-web-services amazon-s3 cors

在使用 canvas 保存图像时遇到问题(Chrome、Firefox 和 Safari),

当我在 html5 Canvas 中加载托管图像时, Canvas 中显示错误(尽管仅在 Chome 而非 Firefox 中),图像来自 Amazon S3 服务器:

Image from origin 'https://xxx.s3.amazonaws.com' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9015' is therefore not allowed access.

当我尝试将 Canvas 保存为图像时,

Uncaught SecurityError: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

是的,我已经设置了 CORS 配置,

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

img.crossOrigin = 'something'

然后我查看了Respond Proxy Js ,但无法理解如何使用它。

我已经查看了很多 SO 帖子和其他引用资料,但无法解决问题。任何人都可以为我提供任何解决方案。

@Update:如果我更改行 <AllowedOrigin>*</AllowedOrigin> => <AllowedOrigin>http://localhost:9015</AllowedOrigin> , 解决方案似乎允许Chrome保存 Canvas 图像,但问题在Firefox和Safari中仍然存在

最佳答案

在 firefox 和 IE 中你需要看起来像

<img src="https://platform3-preit-image.s3.amazonaws.com/components/fireside2.png" crossOrigin="anonymous" class="draggable-image" />

在 chrome 中它需要没有 crossOrigin。如果您在 chrome 中包含 crossOrigin 属性,它将失败。

<img src="https://platform3-preit-image.s3.amazonaws.com/components/fireside2.png" class="draggable-image" />

所以基本上你需要根据浏览器呈现两个不同的 html 输出。

关于html - Amazon S3 图像,无法使用 html5 Canvas 保存,出现 Tainted Canvases 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23804608/

相关文章:

java - 安卓绘图程序

performance - 使用 nginx 作为代理时性能不佳

java - 如何在 Android Canvas 中创建颜色图案

javascript - 如何识别手绘形状与 SVG 模型相似

amazon-web-services - AWS 和 Azure 混合云设置 - 此配置是否可行(Azure 负载均衡器 -> AWS VM)?

java - 对 AWS Spring Boot 配置文件进行单元测试 - 可能吗?

javascript - React JS 自定义组件动画

javascript - 如何使用 JQuery 检索单击对象内具有特定类的 span 元素内的文本?

html - 空 bool HTML 属性是否与没有值属性相同?

html - 如何在浏览器中播放带有 .mov 扩展名的电影