S3 not returning Access-Control-Allow-Origin headers?

前端 未结 4 1041
北海茫月
北海茫月 2020-12-13 06:22

I am having trouble forcing S3 to set CORS headers on all of the objects it returns from a bucket, though CORS is enabled, as client-side S3 uploads is working, the returned

4条回答
  •  没有蜡笔的小新
    2020-12-13 07:06

    First of all, make sure an Origin header with every request. If no Origin header is sent, S3 won't send access-control headers, as S3 deems them irrelevant (and typically, they are). A browser (for which the CORS mechanism is meant) will automatically send an Origin header when doing cross-origin HTTP requests through XMLHTTPRequest.

    In case of loading images with img, you need to add crossorigin="anonymous" attribute. See MDN Documentation on crossorigin attribute. This will cause the browser to send an Origin request header like it does with XMLHTTPRequest.

    Going by the answer of Sam Selikoff, you may need to change

     http://*
    

    to

     http://*
     https://*
    

    I haven't tested this.

    Going by Paul Draper's comment on this answer: Watch out for caching issues. A browser may use a cached response that did not include the appropriate Access-Control response headers. During development, you can clear your cache. In production, you must switch to a new URL for the resource, if it was used in a static manner before.

提交回复
热议问题