Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross Origin amazon S3 not working using chrome

Our website is having a problem, with chrome, loading images from amazon S3 with crossOrigin attribute setted on "Anonymous".

Our S3 server is setted with:

`

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <ExposeHeader>x-amz-server-side-encryption</ExposeHeader>
        <ExposeHeader>x-amz-request-id</ExposeHeader>
        <ExposeHeader>x-amz-id-2</ExposeHeader>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

`

I'm using canvg.js in order to create a canvas from an SVG having remote images (on amazon S3 server) but chrome browser returns me "No 'Access-Control-Allow-Origin' header is present on the requested resource." error after executed this code:

 this.img = document.createElement('img');
            var self = this;
            this.img.onload = function() { self.loaded = true; }
            this.img.onerror = function() { if (typeof(console) != 'undefined')                                            
            console.log('ERROR: image "' + href + '" not found'); self.loaded = true; } }
            if (svg.opts['useCORS'] == true) { 
                                this.img.crossOrigin = 'Anonymous'; }
            this.img.src = href;

In firefox and IE this doesn't cause any issue.

like image 960
user3642612 Avatar asked Mar 17 '26 11:03

user3642612


1 Answers

This is an issue with Chrome caching requests. Here is a discussion on the topic.

Simply add the following to all HTML tags where you are trying to access resources from a different domain crossorigin="anonymous" as documented by MDN here.

like image 150
rmlarsen Avatar answered Mar 18 '26 23:03

rmlarsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!