react - 从 s3 获取图像时,请求的资源上不存在“Access-Control-Allow-Origin”标头

时间:2021-02-28 08:50:30

标签: reactjs amazon-web-services amazon-s3 cors

import imageToBase64 from 'image-to-base64';
...

formatImageToBase64 = () => {
   imageToBase64("https://example.s3.ap-southeast-1.amazonaws.com/images/image1") // Path to the image
       .then(
           (response) => {
               console.log(response);
           }
       )
       .catch(
           (error) => {
               console.log(error); 
           }
       )
}

当我运行这个函数时,出现如下错误。

Access to fetch at 'example.s3.ap-southeast-1.amazonaws.com/images/image1?AWSAccessKeyId=qwertyuip12344&Expires=1664366635&Signature=aoqogfjfj3%2Foofofol%3D' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled

我检查了我的 S3 设置,但似乎没有问题。

enter image description here 我也尝试将 AllowedOrigin 更改为“*”,但仍然无法正常工作。

1 个答案:

答案 0 :(得分:0)

尝试使用 <AllowedOrigin>*</AllowedOrigin> 代替您的 localhost 并检查它是否有效 - 如果有效,则您知道问题在 AllowedOrigin 规则内。

有关如何定义 CORS 的更详细说明,请查看:S3 - Access-Control-Allow-Origin Header

更新:我已尝试访问您提供的链接,我得到的回报是以下消息“您尝试访问的存储桶必须使用指定的端点进行寻址。请发送所有未来的请求到这个端点”。 您可以通过以下网址跟进:AWS S3: The bucket you are attempting to access must be addressed using the specified endpoint