Skip to content

Cloudflare R2 client initialization fails for EU region buckets due to incorrect endpoint URL #1088

@muhammadzadeh

Description

@muhammadzadeh

📜 Description

The application uses Cloudflare R2 for file uploads via the AWS S3 SDK. The R2 client is initialized with a hardcoded endpoint URL that only works for US/default region buckets. When a bucket is created in the EU region, all upload operations fail because the endpoint URL format is different.

Technical Details :

  • The endpoint is hardcoded in libraries/nestjs-libraries/src/upload/r2.uploader.ts (line 26)
  • Current endpoint: https://${CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com
  • Required EU endpoint: https://${CLOUDFLARE_ACCOUNT_ID}.eu.r2.cloudflare.com
  • This affects all R2 operations: simple uploads, multipart uploads, presigned URLs, etc.

👟 Reproduction steps

  1. Set up Cloudflare R2 bucket in EU region:
  • Create a Cloudflare R2 bucket with jurisdiction set to "EU"
  • Note your Account ID, Access Key, and Secret Access Key
  1. Configure the application environment variables:
  1. Attempt to upload a file:
  • Start the application
  • Navigate to any upload functionality (e.g., media upload, profile picture, etc.)
  • Select a file and attempt to upload
  • Observe the upload fails
  1. Check the application logs:
  • Look for error messages related to R2/S3 operations
  • Errors is NoSuchBucket: The specified bucket does not exist.

👍 Expected behavior

  • The file upload should succeed regardless of the bucket region (US, EU, APAC, etc.)
  • The S3 client should connect to the correct regional endpoint
  • Upload operations (simple upload, multipart upload, presigned URLs) should work correctly
  • Users should see a success message and the uploaded file should be accessible
  • The application should return the correct file URL

👎 Actual Behavior with Screenshots

  • Upload operations fail for EU region buckets
  • The S3 client attempts to connect to https://{account_id}.r2.cloudflarestorage.com (default endpoint)
  • Connection fails or returns authentication/access errors because the bucket exists on the EU endpoint
  • Error messages in console logs show S3/R2 operation failures
  • Users see upload error messages or timeouts
  • Files are not uploaded to R2
Image Image Image

💻 Operating system

Linux

🤖 Node Version

22.20-alpine

📃 Provide any additional context for the Bug.

No response

👀 Have you spent some time to check if this bug has been raised before?

  • I checked and didn't find similar issue

Are you willing to submit PR?

Yes I am willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions