# Configured Cloudflare R2 for both uploads and backups in Discourse forum

**URL:** <https://forum.hhf.technology/t/configured-cloudflare-r2-for-both-uploads-and-backups-in-discourse-forum/259>\
**Category:** Self-hosted Applications\
**Tags:** discourse, cloudflare-r2\
**Created:** [October 18, 2024, 5:48pm UTC](https://forum.hhf.technology/t/configured-cloudflare-r2-for-both-uploads-and-backups-in-discourse-forum/259 "2024-10-18T17:48:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hhf.technoloy](https://forum.hhf.technology/user_avatar/forum.hhf.technology/hhf.technoloy/32/68_2.png) [@hhf.technoloy](https://forum.hhf.technology/u/hhf.technoloy)\
**Post date:** [October 18, 2024, 5:48pm UTC](https://forum.hhf.technology/t/configured-cloudflare-r2-for-both-uploads-and-backups-in-discourse-forum/259/1 "2024-10-18T17:48:14Z")

</div>

### 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:

```auto
[
  {
      "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!
