diff --git a/src/app/config/config.ts b/src/app/config/config.ts index 1e9b268c05..5344bc45a7 100644 --- a/src/app/config/config.ts +++ b/src/app/config/config.ts @@ -81,13 +81,17 @@ const s3BucketUrlVars = convict(s3BucketUrlSchema) .validate({ allowed: 'strict' }) .getProperties() +const hasR2Buckets = Object.values(s3BucketUrlVars).some((url) => + /https:\/\/\w+.r2.cloudflarestorage.com/i.test(url), +) + const s3 = new aws.S3({ region: basicVars.awsConfig.region, // Unset and use default if not in development mode // Endpoint and path style overrides are needed only in development mode for // localstack to work. endpoint: isDev ? s3BucketUrlVars.endPoint : undefined, - s3ForcePathStyle: isDev ? true : undefined, + s3ForcePathStyle: isDev || hasR2Buckets ? true : undefined, }) // using aws-sdk v3 (FRM-993) diff --git a/src/app/config/schema.ts b/src/app/config/schema.ts index 5f8cde55c5..704a8b1917 100644 --- a/src/app/config/schema.ts +++ b/src/app/config/schema.ts @@ -59,8 +59,11 @@ const validateS3BucketUrl = ( } // Region should be specified correctly in production const isRegionCorrect = new RegExp(`^https://s3.${region}.amazonaws.com`, 'i') - if (!isDev && !isRegionCorrect.test(val)) { - throw new Error(`region should be ${region}`) + const isR2 = new RegExp(`^https://\\w+.r2.cloudflarestorage.com`, 'i') + if (!isDev && !isRegionCorrect.test(val) && !isR2.test(val)) { + throw new Error( + `region should be ${region}, or url should be for Cloudflare R2`, + ) } /* eslint-enable typesafe/no-throw-sync-func */ }