我在Heroku处设置了服务器,并且我使用了Amazon s3进行图像存储(使用carrierwave gem上传了它们),但有时我会遇到奇怪的403错误:
“无法加载资源:服务器响应状态为403(禁止)”
如果我重新加载页面,则会再次显示图像。
我已经设置了一个CORSRule:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<ExposeHeader>ETag</ExposeHeader>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<ExposeHeader>Accept-Ranges</ExposeHeader>
<ExposeHeader>Content-Range</ExposeHeader>
<ExposeHeader>Content-Encoding</ExposeHeader>
<ExposeHeader>Content-Length</ExposeHeader>
<AllowedHeader>Range</AllowedHeader>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
我不是CORSRule的专家,我尝试了一些,但如果有人能指出我正确的方向,我会很高兴。
当我丢失图像时,我的请求常规,响应标题和请求标题。
Request URL:https://exnerbilleder.s3.amazonaws.com/nye/uploads/image/image/215/thumb_5.jpg
Request Method:GET
Status Code:403 Forbidden
Remote Address:52.95.150.17:443
Referrer Policy:no-referrer-when-downgrade
Content-Type:application/xml
Date:Tue, 08 Aug 2017 15:49:49 GMT
Server:AmazonS3
Transfer-Encoding:chunked
x-amz-id-2:EG4D5X29x8eAuu+xD9PdG2MsdGYnWHnpkuOc87p1h7nbFgvk57S6Be631UwTdF9tHMauoAjf6j4=
x-amz-request-id:0C43705C41CD57C9
Accept:image/webp,image/apng,image/*,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:exnerbilleder.s3.amazonaws.com
Referer:http://inger-exner.herokuapp.com/works/slotsgarden
User-Agent:Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Mobile Safari/537.36
如果您访问the site
,也许会遇到问题答案 0 :(得分:1)
看起来像亚马逊问题。 New Relic或Skylight等工具可以帮助您调试此类问题,其中涉及不同服务提供商的不同组件。
答案 1 :(得分:1)
试试这个:
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>http://www.example.com</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<ExposeHeader>x-amz-server-side-encryption</ExposeHeader>
<ExposeHeader>x-amz-request-id</ExposeHeader>
<ExposeHeader>x-amz-id-2</ExposeHeader>
</CORSRule>
</CORSConfiguration>
您可以参考以下两个链接获取帮助:
http://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html#how-do-i-enable-cors
https://devcenter.heroku.com/articles/direct-to-s3-image-uploads-in-rails