imgタグに動的にS3に置いてある画像を割り当てて表示しようとしています。
以下のエラーがなかなか解決できません。
Access to Image at 'https://s3-ap-northeast-1.amazonaws.com/xxxxx-test/logo.png' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
ソースコードは以下です。
(React内のコードになります。)
img.crossOrigin = "Anonymous";をつけているのですが、、、
let img = document.createElement("img"); img.style.display = 'none'; img.crossOrigin = "Anonymous"; img.onload = () => { let canvas = document.createElement('canvas'); canvas.width = img.width; canvas.height = img.height; var ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0); senderLogo.setAttribute("xlink:href", canvas.toDataURL("image/png")); this.showSvgData(dummySvg.outerHTML, templatePage.id); document.body.removeChild(dummyDiv); } img.src = this.props.senderInfo.logo_path;
S3側でもCORSの設定で許可してあります。
以下のように返ってきているので、問題無いと思っています。
$ curl -H "Access-Control-Request-Method: GET" -H "Origin: http://localhost" --head https://s3-ap-northeast-1.amazonaws.com/xxxxx-test/logo.png HTTP/1.1 200 OK x-amz-id-2:xxxxx x-amz-request-id: FECB4123F75FF571 Date: Wed, 27 Jun 2018 07:11:55 GMT Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET Access-Control-Max-Age: 3000 Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method Last-Modified: Thu, 14 Jun 2018 05:29:08 GMT ETag: "aa3ccba75ab8081bf2668525f0a339a5" Accept-Ranges: bytes Content-Type: image/png Content-Length: 25423 Server: AmazonS3
当該バケットのCORSポリシーは以下のようになっています。
<?xml version="1.0" encoding="UTF-8"?> <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <CORSRule> <AllowedOrigin>*</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> <MaxAgeSeconds>3000</MaxAgeSeconds> <AllowedHeader>*</AllowedHeader> </CORSRule> </CORSConfiguration>
もし、何かお気づきのところがあれば、アドバイスいただけると幸いです。
よろしく御願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。