Skip to content

Instantly share code, notes, and snippets.

@yuna0x0
Last active January 1, 2023 19:06
Show Gist options
  • Save yuna0x0/9d66d9271a001027f3f6575e6f1ae9f5 to your computer and use it in GitHub Desktop.
Save yuna0x0/9d66d9271a001027f3f6575e6f1ae9f5 to your computer and use it in GitHub Desktop.
Cloudflare R2 - Static CDN CORS (allow GET and HEAD from any origin)

Make sure AWS CLI is installed: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

  1. Login to AWS CLI using Cloudflare R2 Token: https://developers.cloudflare.com/r2/examples/aws-cli/
$ aws configure
AWS Access Key ID [None]: <access_key_id>
AWS Secret Access Key [None]: <access_key_secret>
Default region name [None]: auto
Default output format [None]: json
  1. Get current bucket CORS policy
$ aws s3api get-bucket-cors --bucket <BUCKET_NAME> --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com
  1. Set bucket CORS policy according to s3-cors.json
$ aws s3api put-bucket-cors --bucket <BUCKET_NAME> --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com --cors-configuration "file:///<PATH_TO_s3-cors.json>"
{
"CORSRules": [
{
"AllowedMethods": [
"GET",
"HEAD"
],
"AllowedOrigins": [
"*"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment