0.1.5 • Published 1 year ago

@mettle-studio/strapi-provider-upload-cloudflare-2 v0.1.5

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

strapi-provider-upload-cloudflare-2

npm version npm downloads

Non-Official Cloudflare Images and Video Upload Provider for Strapi

This has only been tested with Strapi 4.

Installation

# using yarn
yarn add strapi-provider-upload-cloudflare-2

# using npm
npm install strapi-provider-upload-cloudflare-2

Configurations

Your configuration is passed down to the provider.

See the using a provider documentation for information on installing and using a provider. And see the environment variables for setting and using environment variables in your configs.

Provider Configuration

./config/plugins.js

module.exports = ({ env }) => ({
  // ...
  upload: {
    config: {
      provider: 'strapi-provider-upload-cloudflare-2',
      providerOptions: {
        accountId: env('STRAPI_UPLOAD_CLOUDFLARE_ACCOUNT_ID'),
        apiKey: env('STRAPI_UPLOAD_CLOUDFLARE_API_KEY'),
        variant: 'cms',
      },
    },
  },
  // ...
});

For the variant you can use one you have configured in Cloudflare Images. Or if not, can configure. In this example we used 'cms' as variant. Creating variants of your image is partly explained in the Cloudflare documentation. For this go to your Cloudflare Dash panel and click Images. Next click Variants. From that screen you can add a new variant. Please note that with the Fit Scale Down option your image size will keeps its original size as long as its not above the given width and height parameters.

Environment configuration

In the Provider Configuration there are the accountId and apiKey. Those environment variables should be set in the .env file . To find your Cloudflare accountId and apiKey, log into your Cloudflare dashboard and click "Images". On that page you should see "Account ID" under Developer Resources. For apiKey, if you click the "Use API" tab you'll see a link next to "API Token" to generate an apiKey. Click get started at Create Custom Token and give your token a name e.g. 'Cloudflare images Strapi'. In the permissions section after Account select item choose "Cloudflare Images" with edit permission. Next add another one and after Account select item choose "Stream" with edit permission.

./.env

STRAPI_UPLOAD_CLOUDFLARE_ACCOUNT_ID=<place your Cloudflare Account ID here>
STRAPI_UPLOAD_CLOUDFLARE_API_KEY=<place your Cloudflare apiKey here>

Security Middleware Configuration

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

./config/middlewares.js

module.exports = [
  // ...
  {
    name: 'strapi::security',
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          'connect-src': ["'self'", 'https:'],
          'img-src': ["'self'", 'data:', 'blob:', 'imagedelivery.net'],
          'media-src': ["'self'", 'data:', 'blob:', 'imagedelivery.net'],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  // ...
];

Optional

By default Strapi comes with Responsive formats and provides large, medium and small sizes automatically which also will be uploaded to Cloudflare images. This will count through your Cloudflare images plan by the limit of the amount of images. Because the variants in cloudflare images comes included in the plan it is recommended to turn off the responsive formats. To turn this off open your Strapi admin dashboard and go to settings at global settings choose Media Library. In this screen you can turn off Responsive friendly upload and Size optimization since this is handled by Cloudflare images as well. Please note Strapi still comes with a thumbnail image variant which is also uploaded automatically to Cloudflare images.

Resources

Links