Image thumbnail is broken on chat messages when using Cloudflare R2

AI-generated summary

Users RoldanLT and MarkoK report that image thumbnails in chat messages are broken when using Cloudflare R2 storage. While clicking the broken thumbnail successfully loads the full image, the thumbnail itself returns an InvalidArgument authorization error. The issue stems from the system generating URLs using the raw R2 endpoint (e.g., bucket.xxxxx.r2.cloudflarestorage.com) instead of the configured CDN URL (e.g., r2.discourse.com or cdn.pelittaa.fi). RoldanLT notes this occurs for both chat attachments and theme assets, providing a configuration snippet where DISCOURSE_S3_CDN_URL is set correctly but seemingly ignored for thumbnails. Justin_Hamilton confirms experiencing the same issue on their instance. MarkoK suggests a workaround and links to a detailed explanation and fix on the Discourse Meta forum: Cloudflare R2 image URL display issue detailed explanation and fix.

I’m using Cloudflare R2, by the way, and it works great, except for this issue.
The only issue I encounter is images attached on chat; the thumbnail is broken.

Clicking the thumbnail loads the full images correctly.

The thumbnail URL that is broken is
https://bucket.xxxxxx.r2.cloudflarestorage.com/optimized/1X/xxxxx_2_690x201.png

Which, when accessed directly, returns:

<Error>
<Code>InvalidArgument</Code>
<Message>Authorization</Message>
</Error>

Thumbnail correct URL should be:
https://r2.discourse.com/optimized/1X/xxxxx_2_690x201.png

The full image URL is
https://r2.discourse.com/original/1X/xxxxx.png

It seems for image thumbnails it uses the Cloudflare R2 endpoint API domain for some reason.

My app.yml config:

  ## Cloudflare R2
  DISCOURSE_USE_S3: true
  DISCOURSE_S3_REGION: auto
  DISCOURSE_S3_ACCESS_KEY_ID: xxxxx
  DISCOURSE_S3_SECRET_ACCESS_KEY: xxxxx
  DISCOURSE_S3_CDN_URL: https://r2.discourse.com
  DISCOURSE_S3_BUCKET: bucket
  DISCOURSE_S3_ENDPOINT: https://xxxxx.r2.cloudflarestorage.com
  DISCOURSE_S3_INSTALL_CORS_RULE: false
  DISCOURSE_S3_BACKUP_BUCKET: bucket/backups
  DISCOURSE_BACKUP_LOCATION: s3

Thanks, and I apologize for being a newbie on this thing.

The same issue happens for newly installed themes; the thumbnail is broken and points to
https://bucket.xxxxx.r2.cloudflarestorage.com/original/1X/xxxxx.png

Did you ever find a resolution to this? I also have this issue on my own instance.

Now that I am too utilizing chat I noticed this error too.
Thumbnail is indeed broken here and when I click the broken image it shows the image in a “popup” as it should.

The url for the broken image is indeed not the CDN url.

Broken url (redacted some bits)

https://BUCKET.REDACTED.eu.r2.cloudflarestorage.com/optimized/1X/21ec5908f511bdc9fa7970107b40fad3f008327c_2_500x500.jpeg

Working url

https://cdn.pelittaa.fi/optimized/1X/21ec5908f511bdc9fa7970107b40fad3f008327c_2_500x500.jpeg

This seems to be a “fix” for this issue, or at least a workaround to get this working for now. Ping @Justin_Hamilton