Because as Kian mentioned the SPA does
Because as Kian mentioned the SPA does not have a server so the cors approach would never really work as the 'client' can't really respond (if i got it correctly)
7 Replies
Setting up a bucket's CORS policies is a one time thing only. You can do it from your local machine instead of trying to do it from your app
That's what Kian was sayng. You cannot set up CORS from a browser because you would need CORS set up in the first place to be able to do that. Chicken-and-egg
Oh, so I can setup cors one time and then the bucket keeps those settings?
How should I do that?
So its a matter of configuring the bucket then?
Yep! So like I said, the only way to configure CORS is via the S3 API. So you'd have to set up the S3 SDK locally. Then it is a matter of sending out a PutBucketCors command.
A CORS UI is in the works and will arrive soon, but it doesn't exist yet
This is what that could look like:
I have set the bucket cors via
aws s3api put-bucket-cors --bucket <bucketName> --cors-configuration file://cors.json --endpoint-url https://<accountid>.r2.cloudflarestorage.com
and just checked via aws s3api get-bucket-cors --bucket <bucketName> --endpoint-url https://<accountid>.r2.cloudflarestorage.com
I get back
as the defined bucket-cors
and yet still get the ...from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Any idea on how to address this?
nevermind. Silly mistake on my end. changed localhost:5173
into http:localhost:5173
Oh um, you shouldn't need that
http:
it didn't work without it
and now everything is working as I would expect it to!!
Thank you a lot for you extremely useful guidance!!!
Perhaps a useful thing to add to the docs currently (as the UI is not yet working)
- Add this to
package.json
inside the scripts
- Replace <BucketName>
with your R2 bucket name
- Replace <UserID>
with your cloudflare User ID
- Create a cors.json
file with your desired CORS
settings
- To apply the CORS
rules do:
- npm run cors:set
(To set the CORS rules)
- npm run cors:get
(To check if it applied correctly)Oh that's interesting, but glad it is working! The UI, along with a docs update specifically for CORS is coming very soon though!
Actually, you know what, you are totally correct about needing
http://
BTW