简体   繁体   English

通过JS加载图像的S3存储桶CORS错误

[英]S3 bucket CORS error for loading image via JS

I have an s3 bucket setup with public files. 我有一个带有公共文件的S3存储桶设置。 This is the CORS config for this bucket - 这是该存储桶的CORS配置-

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/938934/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>

In my html webpage, I am trying to access an image file from this bucket, and I am able to render image on my webpage via s3 bucket that - 在我的html网页中,我试图从此存储桶访问图像文件,并且能够通过s3存储桶在网页上呈现图像,

<img src="bucketurl/abcd"/>

But, when I am trying to load that image via javascript, it is not loading and giving cors error. 但是,当我尝试通过javascript加载该图像时,它没有加载并显示cors错误。 (this is a third party plugin code, which cannot bypass CORS. http://html2canvas.hertzen.com/ ) - (这是第三方插件代码,不能绕过CORS。http: //html2canvas.hertzen.com/ )-

var imageLoadHandler = function imageLoadHandler(supportsDataImages) {
    return new Promise(function (resolve, reject) {
        var img = new Image();
        img.onload = function () {
            return resolve(img);
        };
        //ios safari 10.3 taints canvas with data urls unless crossOrigin is set to anonymous
        if (!supportsDataImages || useCORS) {
            img.crossOrigin = 'anonymous';
        }

        img.onerror = reject;
        img.src = src;
        if (img.complete === true) {
            // Inline XML images may fail to parse, throwing an Error later on
            setTimeout(function () {
                resolve(img);
            }, 500);
        }
        if (_this4.options.imageTimeout) {
            var timeout = _this4.options.imageTimeout;
            setTimeout(function () {
                return reject( true ? 'Timed out (' + timeout + 'ms) fetching ' + src.substring(0, 256) : '');
            }, timeout);
        }
    });
};

This is the error - 这是错误-

Access to image at 'https://bucketurl/abcd' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I dont want to open resources from all origins. 我不想开放所有来源的资源。 I just want to open resources from s3bucket. 我只想从s3bucket打开资源。 How and what change needs to be done for this. 为此需要如何以及进行哪些更改。

Your current CORS configuration allows GET , HEAD , and POST requests from all origins. 您当前的CORS配置允许来自所有来源的GETHEADPOST请求。

However, some browsers do send a preflight request 1 to verify what HTTP requests are supported by a server. 但是,某些浏览器确实会发送预检请求1,以验证服务器支持哪些HTTP请求。

The preflight request is a OPTIONS request and this is performed with specific headers. 预检请求是一个OPTIONS请求,它是通过特定的标头执行的。 Your current configuration has only preflight requests with the Authorization header allowed 您当前的配置仅具有允许Authorization标头的预检请求
This means that browsers like Google Chrome that send out a preflight request will fail on this note. 这意味着发出预检请求的浏览器(例如Google Chrome)将在此注释上失败。

You should include entries for all headers sent in the preflight request as an allowed header in your CORS configuration. 您应该将预检请求中发送的所有标头的条目作为允许的标头包含在CORS配置中。 2 2

I suggest restricting CORSRule to only origins that you've considered. 我建议将CORSRule仅限于您考虑过的来源。 Keep tweaking the rules till you come upon a restrictive policy that fits your application. 不断调整规则,直到找到适合您的应用程序的限制性策略为止。 3 3

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/938934/">
<CORSRule>
    <AllowedOrigin>http://localhost:8080</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Authorization</AllowedHeader>
    <AllowedHeader>/replace with another preflight header/</AllowedHeader>
</CORSRule>
<CORSRule>
    <AllowedOrigin>http://html2canvas.herzen.com</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>OPTIONS</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Authorization</AllowedHeader>
    <AllowedHeader>/replace with another preflight header/</AllowedHeader>
</CORSRule>
</CORSConfiguration>

You can view the response headers for the preflight request, when you inspect it in the network tab of your browser. 在浏览器的“网络”标签中检查预检请求时,可以查看该响应标头。

You can also specify a wildcard to match allow all headers in the preflight request. 您还可以指定一个通配符以匹配允许预检请求中的所有标头。

    <AllowedHeader>*</AllowedHeader>

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

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