# Cloudflare R2: Navigating Setup and Handling Configuration Errors

**URL:** https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617
**Category:** Self-hosting
**Tags:** s3
**Created:** [April 21, 2024, 9:13pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617 "2024-04-21T21:13:01Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 1, 2024, 4:01pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/22 "2024-05-01T16:01:05Z")

</div>

> [@kuaza](#):
>
> ```plaintext
> after_assets_precompile:
> 
> ```

That line is not indented properly. It should have 2 spaces exactly like `after_code`

---

<div class="post-metadata">

### Author: ![kuaza](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kuaza/32/264151_2.png) [@kuaza](https://meta.discourse.org/u/kuaza)
#### Post date: [May 1, 2024, 4:05pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/23 "2024-05-01T16:05:46Z")

</div>

Is this a problem? because some settings start without spaces. and when I compile it doesn’t give any error. Example:

 ![image](https://global.discourse-cdn.com/meta/original/4X/1/d/6/1d608a9ade36b0226a3b756b0ccc4fc8e1cb8615.png)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 1, 2024, 5:03pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/24 "2024-05-01T17:03:18Z")

</div>

That all looks fine. What does the `after_assets_precompile` part that I asked about look like?

---

<div class="post-metadata">

### Author: ![kuaza](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kuaza/32/264151_2.png) [@kuaza](https://meta.discourse.org/u/kuaza)
#### Post date: [May 1, 2024, 5:13pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/25 "2024-05-01T17:13:44Z")

</div>

![image](https://global.discourse-cdn.com/meta/original/4X/5/d/c/5dcbf7040f149c08702e25752d01603cea1305c9.png)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 1, 2024, 5:16pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/26 "2024-05-01T17:16:25Z")

</div>

> [@pfaffman](#):
>
> That line is not indented properly. It should have 2 spaces exactly like `after_code`

You need to insert two spaces in front of “after\_assets\_precompile”.

---

<div class="post-metadata">

### Author: ![kuaza](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kuaza/32/264151_2.png) [@kuaza](https://meta.discourse.org/u/kuaza)
#### Post date: [May 1, 2024, 5:17pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/27 "2024-05-01T17:17:08Z")

</div>

I will do this and let you know. Thank you so much for your time \<3

---

<div class="post-metadata">

### Author: ![kuaza](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kuaza/32/264151_2.png) [@kuaza](https://meta.discourse.org/u/kuaza)
#### Post date: [May 1, 2024, 11:32pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/28 "2024-05-01T23:32:20Z")

</div>

When I apply this, it gives the following error and it does not load, the site cannot be reached:

```plaintext
FAILED
--------------------
Pups::ExecError: cd /var/www/discourse && sudo -E -u discourse bundle exec rake s3:upload_assets failed with return #<Process::Status: pid 4803 exit 1>
Location of failure: /usr/local/lib/ruby/gems/3.2.0/gems/pups-1.2.1/lib/pups/exec_command.rb:132:in `spawn'
exec failed with the params {"cd"=>"$home", "cmd"=>["sudo -E -u discourse bundle exec rake s3:upload_assets", "sudo -E -u discourse bundle exec rake s3:expire_missing_assets"]}
bootstrap failed with exit code 1
**FAILED TO BOOTSTRAP** please scroll up and look for earlier error messages, there may be more than one.
./discourse-doctor may help diagnose the problem.

```

./discourse-doctor I ran this, then recompiled, got the same error.

When I enter the container and run the following codes, I get an error:

`rake uploads:migrate_to_s3`

Error:

```plaintext
rake uploads:migrate_to_s3 --trace
Plugin name is 'DiscourseMatheditor', but plugin directory is named 'discourse-matheditor'
Plugin name is 'discourse-post-voting', but plugin directory is named 'discourse-question-answer'
Plugin name is 'discourse-topic-voting', but plugin directory is named 'discourse-voting'
** Invoke uploads:migrate_to_s3 (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute uploads:migrate_to_s3
Please note that migrating to S3 is currently not reversible!
[CTRL+c] to cancel, [ENTER] to continue

Migrating uploads to S3 for 'default'...
Some uploads were not migrated to the new scheme. Running the migration, this may take a while...
rake aborted!
FileStore::ToS3MigrationError: Some uploads could not be migrated to the new scheme. You need to fix this manually. (FileStore::ToS3MigrationError)
/var/www/discourse/lib/file_store/to_s3_migration.rb:156:in `migrate_to_s3'
/var/www/discourse/lib/file_store/to_s3_migration.rb:59:in `migrate'
/var/www/discourse/lib/tasks/uploads.rake:126:in `migrate_to_s3'
/var/www/discourse/lib/tasks/uploads.rake:106:in `block in migrate_to_s3_all_sites'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rails_multisite-6.0.0/lib/rails_multisite/connection_management/null_instance.rb:49:in `with_connection'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rails_multisite-6.0.0/lib/rails_multisite/connection_management/null_instance.rb:36:in `each_connection'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rails_multisite-6.0.0/lib/rails_multisite/connection_management.rb:21:in `each_connection'
/var/www/discourse/lib/tasks/uploads.rake:104:in `migrate_to_s3_all_sites'
/var/www/discourse/lib/tasks/uploads.rake:100:in `block in <main>'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `block in execute'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `each'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `execute'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `synchronize'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:188:in `invoke'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:188:in `invoke_task'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `block (2 levels) in top_level'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `each'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `block in top_level'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:147:in `run_with_threads'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:132:in `top_level'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:83:in `block in run'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:214:in `standard_exception_handling'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:80:in `run'
bin/rake:13:in `<top (required)>'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:58:in `load'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:58:in `kernel_load'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:23:in `run'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:451:in `exec'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/command.rb:28:in `run'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor.rb:527:in `dispatch'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:34:in `dispatch'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/base.rb:584:in `start'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:28:in `start'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/exe/bundle:28:in `block in <top (required)>'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/exe/bundle:20:in `<top (required)>'
/usr/local/bin/bundle:25:in `load'
/usr/local/bin/bundle:25:in `<main>'
Tasks: TOP => uploads:migrate_to_s3

```

Some similar unresolved issues:

> [@S3 error trying to run rake task](https://meta.discourse.org/t/s3-error-trying-to-run-rake-task/274520):
>
> I installed my Discourse, added my S3 creds and added the relevant S3 code into the hooks When rebuilding, I get this error: FAILED -------------------- Pups::ExecError: cd /var/www/discourse && sudo -E -u discourse bundle exec rake s3:upload\_assets failed with return #\<Process::Status: pid 1532 exit 1\> Location of failure: /usr/local/lib/ruby/gems/3.2.0/gems/pups-1.1.1/lib/pups/exec\_command.rb:117:in `spawn' exec failed with the params {"cd"=\>"$home", "cmd"=\>["sudo -E -u discourse bundle exec…

> [@How to debug S3 uploads](https://meta.discourse.org/t/how-to-debug-s3-uploads/213616):
>
> For a brief moment I had S3 uploads to a bucket working. Then I followed the several posts here to make sure it is all correct and now all uploads go local. I cant see any clues in the error log. How does one go about debugging this complex setup? One post suggests you have to do it with mostly environment variables, but the other does not, and the admin panel seems to have all the S3 parameters necessary. I am basically confused. I have double checked all the values for S3 in the system.

> [@Forum rendered unusable](https://meta.discourse.org/t/forum-rendered-unusable/277535/11):
>
> Mmm, I’d noted previously that someone else had talked about putting stuff in the wrong section, then I went ahead and did the same thing anyway, eek (!) So after reading more carefully, I still seem to have an issue, I have; hooks: after\_code: - exec: cd: $home/plugins cmd: - git clone https://github.com/discourse/docker\_manager.git - git clone https://github.com/discourse/discourse-whos-online - git clone https://github.com/discourse/discou…

> [@Defining DISCOURSE\_S3\_CDN\_URL links to assets in S3 CDN URL](https://meta.discourse.org/t/defining-discourse-s3-cdn-url-links-to-assets-in-s3-cdn-url/116798/42):
>
> What am I supposed to do with this? Edit files this way and then rebuild app ? I don’t understand.

---

<div class="post-metadata">

### Author: ![Firepup650](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/firepup650/32/465200_2.png) [@Firepup650](https://meta.discourse.org/u/Firepup650)
#### Post date: [May 2, 2024, 1:22pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/29 "2024-05-02T13:22:16Z")

</div>

> [@kuaza](#):
>
> `Some uploads could not be migrated to the new scheme. You need to fix this manually`

Do any of the other topics you mentioned happen to have instructions for finishing the uploads manually?

> [@kuaza](#):
>
> it gives the following error

That’s just the final fail layer, the actual errors from that would be farther up as it mentions:

> [@kuaza](#):
>
> `please scroll up and look for earlier error messages, there may be more than one.`

---

<div class="post-metadata">

### Author: ![kuaza](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kuaza/32/264151_2.png) [@kuaza](https://meta.discourse.org/u/kuaza)
#### Post date: [May 2, 2024, 8:22pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/30 "2024-05-02T20:22:41Z")

</div>

> [@Firepup650](#):
>
> Do any of the other topics you mentioned happen to have instructions for finishing the uploads manually?

No I did not see. However, there must be a reason why it doesn’t add it after compilation and we should be able to see this error. Maybe I can take a look if it is stated how to see this.

---

<div class="post-metadata">

### Author: ![kuaza](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kuaza/32/264151_2.png) [@kuaza](https://meta.discourse.org/u/kuaza)
#### Post date: [May 3, 2024, 12:51am UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/31 "2024-05-03T00:51:40Z")

</div>

I wanted to try again. I added the necessary S3 connections to app.yml and recomplied them. this time, i removed **after\_assets\_precompile**. All urls changed to cdn and style files were not loaded. When I manually enter the container and try it: **rake s3:upload\_assets** , I get the following error.

```plaintext
Attempting to apply ASSETS S3 CORS ruleset in bucket cloudflarebucketname.
rake aborted!
Aws::S3::Errors::AccessDenied: Access Denied (Aws::S3::Errors::AccessDenied)

```

* * *

If I try this: rake uploads:migrate\_to\_s3 --trace

The error I got:

```plaintext

Migrating uploads to S3 for 'default'...
Some uploads were not migrated to the new scheme. Running the migration, this may take a while...
rake aborted!
FileStore::ToS3MigrationError: Some uploads could not be migrated to the new scheme. You need to fix this manually. (FileStore::ToS3MigrationError)

```

All error message for “rake s3:upload\_assets”:

```plaintext
root@domain-app:/var/www/discourse# rake s3:upload_assets --trace
Plugin name is 'DiscourseMatheditor', but plugin directory is named 'discourse-matheditor'
Plugin name is 'discourse-post-voting', but plugin directory is named 'discourse-question-answer'
Plugin name is 'discourse-topic-voting', but plugin directory is named 'discourse-voting'
** Invoke s3:upload_assets (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke s3:ensure_cors_rules (first_time)
** Invoke environment
** Execute s3:ensure_cors_rules
Installing CORS rules...
Attempting to apply ASSETS S3 CORS ruleset in bucket cloudflarebucketname.
rake aborted!
Aws::S3::Errors::AccessDenied: Access Denied (Aws::S3::Errors::AccessDenied)
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-core-3.191.3/lib/seahorse/client/plugins/raise_response_errors.rb:17:in `call'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-s3-1.143.0/lib/aws-sdk-s3/plugins/sse_cpk.rb:24:in `call'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-s3-1.143.0/lib/aws-sdk-s3/plugins/dualstack.rb:21:in `call'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-s3-1.143.0/lib/aws-sdk-s3/plugins/accelerate.rb:43:in `call'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-core-3.191.3/lib/aws-sdk-core/plugins/checksum_algorithm.rb:111:in `call'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-core-3.191.3/lib/aws-sdk-core/plugins/jsonvalue_converter.rb:16:in `call'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-core-3.191.3/lib/aws-sdk-core/plugins/idempotency_token.rb:19:in `call'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-core-3.191.3/lib/aws-sdk-core/plugins/param_converter.rb:26:in `call'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-core-3.191.3/lib/seahorse/client/plugins/request_callback.rb:89:in `call'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-core-3.191.3/lib/aws-sdk-core/plugins/response_paging.rb:12:in `call'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-core-3.191.3/lib/seahorse/client/plugins/response_target.rb:24:in `call'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-core-3.191.3/lib/seahorse/client/request.rb:72:in `send_request'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/aws-sdk-s3-1.143.0/lib/aws-sdk-s3/client.rb:5424:in `get_bucket_cors'
/var/www/discourse/lib/s3_helper.rb:385:in `fetch_bucket_cors_rules'
/var/www/discourse/lib/s3_helper.rb:166:in `ensure_cors!'
/var/www/discourse/lib/s3_cors_rulesets.rb:60:in `sync'
/var/www/discourse/lib/tasks/s3.rake:184:in `block in <main>'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `block in execute'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `each'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `execute'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `synchronize'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:243:in `block in invoke_prerequisites'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:241:in `each'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:241:in `invoke_prerequisites'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `synchronize'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/task.rb:188:in `invoke'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:188:in `invoke_task'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `block (2 levels) in top_level'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `each'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `block in top_level'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:147:in `run_with_threads'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:132:in `top_level'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:83:in `block in run'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:214:in `standard_exception_handling'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/rake-13.2.1/lib/rake/application.rb:80:in `run'
bin/rake:13:in `<top (required)>'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:58:in `load'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:58:in `kernel_load'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:23:in `run'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:451:in `exec'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/command.rb:28:in `run'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor.rb:527:in `dispatch'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:34:in `dispatch'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/base.rb:584:in `start'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:28:in `start'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/exe/bundle:28:in `block in <top (required)>'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/exe/bundle:20:in `<top (required)>'
/usr/local/bin/bundle:25:in `load'
/usr/local/bin/bundle:25:in `<main>'
Tasks: TOP => s3:upload_assets => s3:ensure_cors_rules

```

It’s hard to get out of this situation.

---

<div class="post-metadata">

### Author: ![kuaza](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kuaza/32/264151_2.png) [@kuaza](https://meta.discourse.org/u/kuaza)
#### Post date: [May 3, 2024, 2:03am UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/32 "2024-05-03T02:03:05Z")

</div>

Something tells me that the S3 codes run after compilation only support Amazon S3. What are you thinking?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 3, 2024, 9:37am UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/33 "2024-05-03T09:37:24Z")

</div>

It works for me. There’s a setting to skip the cors rules, I think. You might look at [Configure an S3 compatible object storage provider for uploads](https://meta.discourse.org/t/configure-an-s3-compatible-object-storage-provider-for-uploads/148916) to see what it is.

---

<div class="post-metadata">

### Author: ![kuaza](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kuaza/32/264151_2.png) [@kuaza](https://meta.discourse.org/u/kuaza)
#### Post date: [May 3, 2024, 9:39am UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/34 "2024-05-03T09:39:15Z")

</div>

this?: DISCOURSE\_S3\_INSTALL\_CORS\_RULE: false

---

<div class="post-metadata">

### Author: ![kuaza](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kuaza/32/264151_2.png) [@kuaza](https://meta.discourse.org/u/kuaza)
#### Post date: [May 3, 2024, 10:08am UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/35 "2024-05-03T10:08:19Z")

</div>

This worked great, but there is one problem. theme-javascripts files are not uploaded to S3. Naturally, the design of the site is damaged. Only the assets folder is loaded. What can we do to upload other design files?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 3, 2024, 11:28am UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/36 "2024-05-03T11:28:27Z")

</div>

This is the last time I’ll tell you that your include the part that calls the rake task to upload to s3.

Good luck.

---

<div class="post-metadata">

### Author: ![kuaza](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kuaza/32/264151_2.png) [@kuaza](https://meta.discourse.org/u/kuaza)
#### Post date: [May 3, 2024, 4:52pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/37 "2024-05-03T16:52:32Z")

</div>

What am I missing? The code below is attached and when I rebuild it loads the assets files but not the theme files.

> [@kuaza](#):
>
> ```plaintext
> after_assets_precompile:
> - exec:
> cd: $home
> cmd:
> - sudo -E -u discourse bundle exec rake s3:upload_assets
> - sudo -E -u discourse bundle exec rake s3:expire_missing_assets
> 
> ```

* * *

When I run this by entering the container: rake uploads:migrate\_to\_s3 I get the following error:

```plaintext
Updating the URLs in the database...
Removing old optimized images...
Flagging all posts containing lightboxes for rebake...
1001 posts were flagged for a rebake
rake aborted!
FileStore::ToS3MigrationError: 9 of 3769 uploads are not migrated to S3. S3 migration failed for db 'default'. (FileStore::ToS3MigrationError)

```

My app.yml file is as follows. I turned off this setting because it did not upload the theme-javascripts and stylesheets: DISCOURSE\_CDN\_URL.

I’ll try to open it again at midnight and compile it, but I don’t know what I missed. I searched to see what else you might mean by this, but I couldn’t find anything different: “the rake task to upload to s3”

```plaintext
  DISCOURSE_USE_S3: true
  DISCOURSE_S3_REGION: 'auto'
  DISCOURSE_S3_ENDPOINT: 'https://xxxxx.eu.r2.cloudflarestorage.com'
  DISCOURSE_S3_ACCESS_KEY_ID: 'xxxxx'
  DISCOURSE_S3_SECRET_ACCESS_KEY: 'xxxxx'
  DISCOURSE_S3_CDN_URL: 'https://cdn.xxxxx.com'
  #DISCOURSE_CDN_URL: 'https://cdn.xxxxx.com'
  DISCOURSE_S3_BUCKET: 'cloudflarer2xxxxx'
  #DISCOURSE_S3_BACKUP_BUCKET: 'cloudflarer2xxxxxbackups'
  #DISCOURSE_BACKUP_LOCATION: 's3'
  DISCOURSE_INCLUDE_S3_UPLOADS_IN_BACKUPS: true
  DISCOURSE_S3_INSTALL_CORS_RULE: false

```

I’m sorry I wasted your time, but it would be nice if the user documentation included these issues. Now we know that r2 works. Just add the following two settings:

```plaintext

DISCOURSE_S3_REGION: 'auto'
DISCOURSE_S3_INSTALL_CORS_RULE: false

```

We can move the images and assets folder on the site to the S3 area. There is only the problem that the theme files and style files are not transferred and I will try it again at midnight. I hope I will be successful and I will mark this issue as solved, of course, after explaining the solution.

Note: Postgresql server is separate, I do not use the one on localhost. I guess there is no problem with this.  
Thank you to everyone who took the time.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 5, 2024, 11:32am UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/38 "2024-05-05T11:32:05Z")

</div>

> [@kuaza](#):
>
> `rake s3:upload_assets`

That is the command to run to upload assets. My guess is that you still have something wrong that’s keeping assets from grin uploaded.

---

<div class="post-metadata">

### Author: ![kuaza](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kuaza/32/264151_2.png) [@kuaza](https://meta.discourse.org/u/kuaza)
#### Post date: [May 5, 2024, 11:50am UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/39 "2024-05-05T11:50:39Z")

</div>

Good, if I’m not doing anything wrong then I need to find the problem. When I enter the container and run the “rake s3:upload\_assets” code, everything seems fine. Even if I turn this setting on or off, it does not load the style files (theme-javascripts and stylesheets): “DISCOURSE\_CDN\_URL: ‘[https://cdn.xxxxx.com](https://cdn.xxxxx.com)’”.

```plaintext
rake s3:upload_assets --trace
Plugin name is 'DiscourseMatheditor', but plugin directory is named 'discourse-matheditor'
Plugin name is 'discourse-post-voting', but plugin directory is named 'discourse-question-answer'
Plugin name is 'discourse-topic-voting', but plugin directory is named 'discourse-voting'
** Invoke s3:upload_assets (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke s3:ensure_cors_rules (first_time)
** Invoke environment
** Execute s3:ensure_cors_rules
Installing CORS rules...
skipping
** Execute s3:upload_assets Skipping: assets/break_string-cc617154cd04790e.js
....
....
....

```

As I said, if I’m not doing something wrong, I try to find the error and how to fix it. If there is any improvement, I will add it here, thank you.

---

<div class="post-metadata">

### Author: ![kamaljain](https://avatars.discourse-cdn.com/v4/letter/k/c2a13f/32.png) [@kamaljain](https://meta.discourse.org/u/kamaljain)
#### Post date: [August 26, 2024, 3:49pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/40 "2024-08-26T15:49:48Z")

</div>

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:

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

---

<div class="post-metadata">

### Author: ![aosus](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@aosus](https://meta.discourse.org/u/aosus)
#### Post date: [October 25, 2024, 7:21pm UTC](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617/41 "2024-10-25T19:21:11Z")

</div>

Did you test if discourse can download files from Cloudflare R2? for example to include S3 uploads in a complete backup of the instance.

> [@Enable hidden setting to include S3 uploads in the backups](https://meta.discourse.org/t/enable-hidden-setting-to-include-s3-uploads-in-the-backups/276535):
>
> bookmark This guide explains how to enable a hidden setting in Discourse to include Amazon S3 (Simple Storage Service) uploads in your backups. Discourse has the ability to store media uploads on Amazon S3 for scalability and reliability. However, these uploads are not included in backups by default. This guide covers enabling a hidden setting to include S3 uploads in backups, with options to configure it via the Rails console or the app.yml file. Using Rails Console To enable S3 uploads…

It doesn’t seem to work because Cloudflare is missing something in the R2 S3 Api, also migrate\_to\_s3 doesn’t work.

[Previous page](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617.md?page=1)

[Next page](https://meta.discourse.org/t/cloudflare-r2-navigating-setup-and-handling-configuration-errors/305617.md?page=3)
