Cloudflare R2: Navigating Setup and Handling Configuration Errors

Hi everyone,

I wanted to share my experience and steps on how I successfully configured Cloudflare R2 for both uploads and backups in my Discourse forum. I hope this helps others who are looking to use Cloudflare R2 with Discourse.

Setup Overview:

  • Platform: Discourse (Bitnami installation)
  • Bucket: Cloudflare R2
  • Custom Domain: Set up for serving files publicly
  • Configuration: Done entirely through Discourse site settings (no environment variables were touched)

Step-by-Step Guide:

  1. Create a Cloudflare R2 Bucket:
  • Log in to your Cloudflare account and navigate to R2 storage.
  • Create a new bucket for your Discourse uploads (e.g., forum-uploads).
  • Note down the Bucket S3 API URL, Access Key ID, and Secret Access Key.
  1. Set Up Custom Domain for CDN:
  • To serve your R2 bucket content publicly, create a custom domain in Cloudflare.
  • For example, I used forumfiles.example.in.
  • Make sure to configure DNS settings to point to Cloudflare’s provided CNAME. Since I have a domain on cloudflare, it did it for me.
  1. Configure Discourse Site Settings:
    • s3_bucket: Your R2 bucket name (e.g., forum-uploads).
    • s3_region: Set as US East (N. Virginia). There was no option of auto.
    • s3_endpoint: Use your Bucket S3 API URL provided by Cloudflare (e.g., https://<your-bucket-unique-id>.r2.cloudflarestorage.com).
    • s3_access_key_id: Your R2 Access Key ID.
    • s3_secret_access_key: Your R2 Secret Access Key.
    • s3_cdn_url: Your custom domain for CDN (e.g., https://forumfiles.example.in).
  • Ensure that enable s3 backups and enable s3 uploads are checked.
  1. Adjust Permissions and CORS:
  • Set your R2 bucket to private and configure a CORS policy:
[
  {
      "AllowedHeaders": ["*"],
      "AllowedMethods": ["GET", "POST", "PUT", "HEAD"],
      "AllowedOrigins": ["*"],
      "ExposeHeaders": ["ETag"],
      "MaxAgeSeconds": 3000
  }
]
  • This ensures that the uploads work correctly without CORS issues.
  1. Testing the Setup:
  • Upload a test file or image through your Discourse forum to verify that uploads are working correctly.
  • Check if the files are accessible via your custom domain (e.g., https://forumfiles.example.in/original/1X/...).
  • Check the backups, I could successfully get backups executed. Cross checked at R2 bucket and its also listed in the site settings.

Conclusion:

By following these steps, I was able to integrate Cloudflare R2 with Discourse seamlessly, handling both uploads and backups efficiently. If you have any questions or run into issues, feel free to ask!

1 Like