Strapi plugin logo for Cloudflare R2 & CDN

Cloudflare R2 & CDN

This upload provider will make it possible to use Cloudflare R2 via Cloudflare CDN public access endpoint.

npm version

strapi-provider-cloudflare-r2

Installation

# using yarn
yarn add strapi-provider-cloudflare-r2

# using npm
npm install strapi-provider-cloudflare-r2 --save

# using pnpm
pnpm add strapi-provider-cloudflare-r2

Configuration

See the documentation about using a provider for information on installing and using a provider. To understand how environment variables are used in Strapi, please refer to the documentation about environment variables.

Provider Configuration

./config/plugins.js or ./config/plugins.ts for TypeScript projects:

1module.exports = ({ env }) => ({
2  // ...
3  upload: {
4    config: {
5      provider: "strapi-provider-cloudflare-r2",
6      providerOptions: {
7        accessKeyId: env("CF_ACCESS_KEY_ID"),
8        secretAccessKey: env("CF_ACCESS_SECRET"),
9        /**
10         * `https://<ACCOUNT_ID>.r2.cloudflarestorage.com`
11         */
12        endpoint: env("CF_ENDPOINT"),
13        params: {
14          Bucket: env("CF_BUCKET"),
15        },
16        /**
17         * Set this Option to store the CDN URL of your files and not the R2 endpoint URL in your DB.
18         * Can be used in Cloudflare R2 with Domain-Access or Public URL: https://pub-<YOUR_PULIC_BUCKET_ID>.r2.dev
19         * This option is required to upload files larger than 5MB, and is highly recommended to be set.
20         * Check the cloudflare docs for the setup: https://developers.cloudflare.com/r2/data-access/public-buckets/#enable-public-access-for-your-bucket
21         */
22        cloudflarePublicAccessUrl: env("CF_PUBLIC_ACCESS_URL"),
23        /**
24         * Sets if all assets should be uploaded in the root dir regardless the strapi folder.
25         * It is useful because strapi sets folder names with numbers, not by user's input folder name
26         * By default it is false
27         */
28        pool: false,
29      },
30      actionOptions: {
31        upload: {},
32        uploadStream: {},
33        delete: {},
34      },
35    },
36  },
37  // ...
38});

Where to find the configuration options
You can find all needed values in the Cloudflare dashboard unter R2. All your buckets, your account ID and the access keys can be found there.

  • endpoint: https://<ACCOUNT_ID>.r2.cloudflarestorage.com
  • accessKeyId: You need to click on Manage R2 API Tokens to create a new token.
  • secretAccessKey: You need to click on Manage R2 API Tokens to create a new token.

Security Middleware Configuration

Due to the default settings in the Strapi Security Middleware you will need to modify the contentSecurityPolicy settings to properly display thumbnail previews in the Media Library. You should replace the strapi::security string with the object below instead as explained in the middleware configuration documentation.

./config/middlewares.js

1module.exports = ({ env }) => [
2  // ...
3  {
4    name: "strapi::security",
5    config: {
6      contentSecurityPolicy: {
7        useDefaults: true,
8        directives: {
9          "connect-src": ["'self'", "https:"],
10          "img-src": [
11            "'self'",
12            "data:",
13            "blob:",
14            "market-assets.strapi.io",
15            env("CF_PUBLIC_ACCESS_URL") ? env("CF_PUBLIC_ACCESS_URL").replace(/^https?:\/\//, "") : "",
16          ],
17          "media-src": [
18            "'self'",
19            "data:",
20            "blob:",
21            "market-assets.strapi.io",
22            env("CF_PUBLIC_ACCESS_URL") ? env("CF_PUBLIC_ACCESS_URL").replace(/^https?:\/\//, "") : "",
23          ],
24          upgradeInsecureRequests: null,
25        },
26      },
27    },
28  },
29  // ...
30];

aws-sdk configuration and AWS_... env variables

As the Clouflare R2 spec follows the AWS S3 spec we make use of aws-sdk package to communicate with Cloudflare R2. Because of this dependency all AWS_... env variables used to configure the aws-sdk are still beeing pulled in by this dependency. If you do not want to configure any special functionality of the aws-sdk then make sure to remove all AWS_... env variables in you deployment.

Bucket CORS Configuration

Do not forget to configure your R2 Endpoint CORS settings as described here: https://developers.cloudflare.com/r2/buckets/cors/

The simplest configuration is to allow GET from all origins:

1[
2  {
3    "AllowedOrigins": ["*"],
4    "AllowedMethods": ["GET"]
5  }
6]

More safe would be to only allow it from your Strapi deployment Origins (better for production):

1[
2  {
3    "AllowedOrigins": ["YOUR STRAPI URL"],
4    "AllowedMethods": ["GET"]
5  }
6]

Sponsors

Strapi Plugin developed and maintained by trieb.work cloud consulting

Install now

npm install strapi-provider-cloudflare-r2

STATS

17 GitHub stars852 weekly downloads

Last updated

191 days ago

Strapi Version

Unavailable

Author

github profile image for Tilman Marquart | trieb.work
Tilman Marquart | trieb.work

Related plugin

Documentation

Useful links

Create your own plugin

Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.