[英]CORS error with S3 even after adding CORS configuration
我正在嘗試通過 javaScript 將圖像上傳到 S3 存儲桶。 提交請求時,返回以下錯誤:
XMLHttpRequest cannot load https://somebucket.s3-us-west
2.amazonaws.com/albums//apicture.png. Response to preflight
request doesn't pass access control check: No 'Access-Control-Allow-Origin'
header is present on the requested resource.
Origin 'http://someorigin:3000' is therefore not allowed access.
我在存儲桶上的 CORS 配置:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>http://someorigin:3000</AllowedOrigin>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
如您所見,我什至手動將原點添加到策略中,以及*
,但似乎沒有任何效果,而且我不確定從哪里開始。
我檢查了 Chrome Devtools 中的網絡窗格,並注意到其中一個 XHR 是apicture.png
,它具有以下關鍵信息:
Request URL:https://somebucket.s3-us-west-2.amazonaws.com/pics/apicture.png
Request Method:OPTIONS
Status Code:301 Moved Permanently
response
選項卡還有一些其他重要信息:
<Error>
<Code>PermanentRedirect</Code>
<Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message>
<Bucket>somebucket</Bucket>
<Endpoint>somebucket.s3-us-west-1.amazonaws.com</Endpoint>
<RequestId>
***************
</RequestId>
</Error>
因此,很明顯,他們希望該區域為us-west-1
但是,如果我將AWS.config
更改為該區域,則會引發不同的錯誤。 亞馬遜自己的指南說“北加州”區域被編碼為us-west-2
,所以我不確定為什么會出現這種不一致。
AWS S3 存儲桶 => 添加元數據“緩存控制”:“無緩存”
現在您需要在 JSON 中進行設置。 權限 -> CORS
[{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": []
}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.