Cloudflare R2: Navigating Setup and Handling Configuration Errors

Is there still no development on cloudflare R2? It looked good to me…

Did you read the note about it above? It’s incompatible with gzip files, and the was no indication that there was a plan to change they. Maybe it works with everything but uploads?

Did you try it? Did it work?

1 Like

Yes, I read it, but it was 2 years ago and some things may have changed.

I wanted to try it today, I activated R2 and added the site. I’m just not sure how to configure this in the discourse step. Because I haven’t been able to find where to get the key or secret codes from Cloudflare yet. I’m experimenting, maybe a little detail can help me :slight_smile:

3 Likes

Idem :thinking: my bucket is active but maybe discourse not has support yet but I hope for this.

1 Like

This page describes how to get the keys: Authentication · Cloudflare R2 docs

2 Likes

I tried everything but I always get this error:

Aws::S3::Errors::BadRequest

I wonder what I’m missing?

When I upload an image from the R2 panel, this image opens with the special cdn url. The problem is that the image cannot be loaded on the site. I created cors and opened all methods.

[
  {
    "AllowedOrigins": [
      "https://SITENAME.com"
    ],
    "AllowedMethods": [
      "GET",
      "POST",
      "HEAD",
      "DELETE",
      "PUT"
    ]
  }
]

any ideas?

1 Like

That’s possible, but the word from Cloudflare 2 years ago was that it wasn’t likely to change, so someone who’s really excited about R2 will need to figure it out. That said, it seems possible that it might work for everything but gz files, which I think means just backups.

But not having s3 backups seems like a lot to give up.

That sounds like you don’t have the right key or the right endpoint.

2 Likes

Frankly, I am currently using cloudflare on my discourse site and it is generally good to have everything in one place. So dealing with amazon can be a bit tiring when you have r2. I tried to install Amazon S3 for a while and for some reason I couldn’t get the style files to load. (okay, I’ll try again) but it would be good not to ignore clouflare, so much so when using the clouflare support forum discourse :slight_smile:

1 Like

That’s usually when you don’t add the stanza that pushes assets to s3 (you’d also need it to make any s3-like service to work if you set the use_s3 variable).

2 Likes

So did you try something else and make it work? What did you do? Can you write it up as the others in Configure an S3 compatible object storage provider for uploads?

Does it work for backups?

I tried everything I could. I tried to do it experimentally by changing the settings. But every time I get this error and when I look at it, it seems that the image has not been loaded.

I couldn’t see anything in the error codes either. It would be nice if I could get it working, having most everything in one panel (cloudflare) would be exciting and effective.

I tried it for backup, sorry. When I failed, I turned to Amazon again. I would be very happy if there was someone more successful than me in this regard :slight_smile:

I uploaded an image from the R2 panel and I can access it. However, I cannot upload an image via the discourse, it gives the above error.

So, maybe it doesn’t work like the documention says that it doesn’t.

1 Like

Or maybe I sort-of owe you and apology. I do seem to have made it work, though I don’t have time to promise that I can write instructions that will allow you to make it work.

  DISCOURSE_USE_S3: true
  DISCOURSE_S3_REGION: 'auto'
  DISCOURSE_S3_ENDPOINT: https://=ACCOUNT_ID=.r2.cloudflarestorage.com
  DISCOURSE_S3_ACCESS_KEY_ID: '=ACCESS_KEY_ID='
  DISCOURSE_S3_SECRET_ACCESS_KEY: '=SECRET_ACCESS_KEY'
  DISCOURSE_S3_CDN_URL: '=S3_CDN_URL='
  DISCOURSE_CDN_URL: '=CDN_URL='
  DISCOURSE_S3_BUCKET: '=BUCKET='
  DISCOURSE_S3_BACKUP_BUCKET: '=BUCKET=/backups'
  DISCOURSE_BACKUP_LOCATION: 's3'

Don’t forget the stanza that does the rake task to push uploads to S3.

GIve this a try and see if it’ll work for you. A quick test shows that it works for uploads, assets, and backups. So, I guess the issue with gzip files no longer applies.

1 Like

After correcting the region (auto) and the last url, it worked. However, there is a problem. When I recreate it, the style files are not loaded and naturally the site does not open. However, when I open it only for upload, I can upload images from the site and we can access it via the cdn url. The only problem is that I want the style files to be served from CDN. How can we do this? I think this is our only problem.

You need to follow the instructions to push the assets to S3:

What did you use for CDN? If it was cloudflare, how did you configure it?

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

I added this. When I recompile, all links on the site change with the cdn url. However, these style files are not uploaded to cloudflare. So I just added the upload region setting to the app.yml file and compiled it that way. When I activate S3 from the admin, the newly uploaded images are uploaded to cloudflare R2 and access is provided.

The only problem is that when I add the s3 settings before compiling the app.yml file, the newly created style files don’t seem to be loaded. But the URLs and cdn urls change on the site.

CORS:

[
  {
    "AllowedOrigins": [
      "https://domain.com"
    ],
    "AllowedMethods": [
      "GET",
      "POST",
      "PUT",
      "HEAD",
      "DELETE"
    ]
  }
]

Could this problem have something to do with Argo? However, images are currently loading without any problems.

To debug, you can enter the container and try to run the rake tasks there and see what happens.

I spent about 4 hours on this yesterday, so I’m afraid my free support on this project is about up.

Don’t know about the CORS stuff. I’d allow everything from everywhere to make sure that’s not the issue. I didn’t do anything woth CORS on my test site.

You’re running this under k8s?

Sorry for taking up your time. If I could help financially I would. I respect your time.

I don’t know what k8s is. I’m working on my own server. and I activated argo. R2 is also active. Everything is working very well now, only after the new compilation it does not load the style files (js, css etc.) into R2. I will re-run the necessary tests later and add them here. If I am successful, I will explain in detail how to find the problem or make it work.

1 Like

What is Argo?

If this is a standard install then you can run the rake command inside the container

./launcher enter app
rake ...

Are you sure that you are including the USE_S3 part? (I guess so, as I think that’s what makes the assets expected on the CDN)

Argo: Overview · Cloudflare Argo Smart Routing docs

I added the USE_S3 setting before compiling, example below:

  DISCOURSE_USE_S3: true
  DISCOURSE_S3_REGION: 'auto'
  DISCOURSE_S3_ENDPOINT: 'https://xxxxxxxxxxxxxxxxx.eu.r2.cloudflarestorage.com'
  DISCOURSE_S3_ACCESS_KEY_ID: 'xxxxxxxxxxxxxxxxx'
  DISCOURSE_S3_SECRET_ACCESS_KEY: 'xxxxxxxxxxxxxxxxx'
  DISCOURSE_S3_CDN_URL: 'https://cdn.domain.com'
  DISCOURSE_CDN_URL: 'https://cdn.domain.com'
  DISCOURSE_S3_BUCKET: 'cloudflaredomain'
  DISCOURSE_S3_BACKUP_BUCKET: 'cloudflaredomain/backups'
  DISCOURSE_BACKUP_LOCATION: 's3'

After recompiling, all urls change as expected. cdn url. The only problem is, these assets are not loaded into R2. I had already added this setting:

  after_code:
     - exec:
        cd: $home/plugins
        cmd:
          - git clone https://github.com/discourse/docker_manager.git

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

Here is the beginning of app.yml:

templates:
  #- "templates/postgres.template.yml"
  - "templates/redis.template.yml"
  - "templates/web.template.yml"
  ## Uncomment the next line to enable the IPv6 listener
  #- "templates/web.ipv6.template.yml"
  #- "templates/web.ratelimited.template.yml"
  ## Uncomment these two lines if you wish to add Lets Encrypt (https)
  # - "templates/web.ssl.template.yml"
  # - "templates/web.letsencrypt.ssl.template.yml"
  - "templates/cloudflare.template.yml"

## which TCP/IP ports should this container expose?
## If you want Discourse to share a port with another webserver like Apache or nginx,
## see https://meta.discourse.org/t/17247 for details
expose:
  - "80:80"   # http
  - "443:443" # https

Now I will try to enter the container and upload the assets to S3.