I am trying to use R2 presigned URL on browser. The request fails due to CORS.
I have faced similar issue on S3 and I know the solution: setting the CORSRule
According to R2 docs, changing CORSRule on dashboard is not supported yet. It is only available via S3 API.
So I did this.
r2.putBucketCors(
{
Bucket: 'game-resources',
CORSConfiguration: {
CORSRules: [
{
AllowedMethods: ['PUT'],
AllowedOrigins: ['*'],
AllowedHeaders: ['*'],
},
],
},
},
(err, data) => {
console.log(err, data)
}
)
and it returns correct CORS Rule when I call this
r2.getBucketCors({ Bucket: 'game-resources' }, (err, data) => {
console.log(err, JSON.stringify(data))
})
{"CORSRules":[{"AllowedHeaders":["*"],"AllowedMethods":["PUT"],"AllowedOrigins":["http://localhost:3000"],"ExposeHeaders":[]}]}
But there’s still 403 CORS error when I try to use it on browser.
I tried using curl
for OPTION request to check what the response throws.
-X 'OPTIONS' \
-H 'Accept: */*' \
-H 'Accept-Language: ko,en;q=0.9,ko-KR;q=0.8,en-US;q=0.7' \
-H 'Access-Control-Request-Headers: content-type' \
-H 'Access-Control-Request-Method: PUT' \
-H 'Connection: keep-alive' \
-H 'Origin: http://localhost:3000' \
-H 'Referer: http://localhost:3000/' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Site: cross-site' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36' \
--compressed
<?xml version="1.0" encoding="UTF-8"?><Error><Code>Unauthorized</Code><Message>CORS not configured for this bucket</Message></Error>
It says CORS not configured for this bucket
.