Skip to content

yclgkd/strapi-provider-upload-tencent-cloud-storage

Repository files navigation

strapi-provider-upload-tencent-cloud-storage

Resources

Links

Installation

# using yarn
yarn add strapi-provider-upload-tencent-cloud-storage

# using npm
npm install strapi-provider-upload-tencent-cloud-storage --save

Configuration

  • provider defines the name of the provider

  • providerOptions is passed down during the construction of the provider. It contains the following properties:

    • SecretId: Tencent Cloud API SecretId
    • SecretKey: Tencent Cloud API SecretKey
    • Region: Tencent Cloud API Region
    • Bucket: Tencent Cloud API Bucket
    • ACL: (optional) ACL applied to the uploaded files.
    • Expires: (optional) Expiration time of the signed URL. Default value is 360 seconds (6 minutes).
    • initOptions: (optional) Options passed to the constructor of the provider. You can find the complete list of options here.
    • uploadOptions: (optional) Options passed to the upload method. You can find the complete list of options here.
    • CDNDomain: (optional) CDN Accelerated Domain.
    • StorageRootPath: (optional) The storage path of the file in the bucket.

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:

module.exports = ({ env }) => ({
  // ...
  upload: {
    config: {
      provider: "strapi-provider-upload-tencent-cloud-storage",
      providerOptions: {
        SecretId: env("COS_SECRET_ID"),
        SecretKey: env("COS_SECRET_KEY"),
        Region: env("COS_REGION"),
        Bucket: env("COS_BUCKET"),
      },
    },
  },
  // ...
});

Configuration for a private COS bucket and signed URLs

If your bucket is configured to be private, you will need to set the ACL option to private in the params object. This will ensure file URLs are signed.

Note: If you are using a CDN, the URLs will not be signed.

You can also define the expiration time of the signed URL by setting the Expires option in the providerOptions object. The default value is 360 seconds (6 minutes).

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

module.exports = ({ env }) => ({
  // ...
  upload: {
    config: {
      provider: "strapi-provider-upload-tencent-cloud-storage",
      providerOptions: {
        SecretId: env("COS_SECRET_ID"),
        SecretKey: env("COS_SECRET_KEY"),
        Region: env("COS_REGION"),
        Bucket: env("COS_BUCKET"),
        ACL: "private", // <= set ACL to private
      },
    },
  },
  // ...
});

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 or ./config/middlewares.ts for TypeScript projects:

module.exports = [
  // ...
  {
    name: "strapi::security",
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          "connect-src": ["'self'", "https:"],
          "img-src": [
            "'self'",
            "data:",
            "blob:",
            "market-assets.strapi.io",
            "yourBucketName.cos.yourRegion.myqcloud.com",
          ],
          "media-src": [
            "'self'",
            "data:",
            "blob:",
            "market-assets.strapi.io",
            "yourBucketName.cos.yourRegion.myqcloud.com",
          ],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  // ...
];

Configure the access domain (CDN acceleration)

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

module.exports = ({ env }) => ({
  // ...
  upload: {
    config: {
      provider: "strapi-provider-upload-tencent-cloud-storage",
      providerOptions: {
        CDNDomain: "example-cdn-domain.com", // <= CDN Accelerated Domain
        SecretId: env("COS_SECRET_ID"),
        SecretKey: env("COS_SECRET_KEY"),
        Region: env("COS_REGION"),
        Bucket: env("COS_BUCKET"),
      },
    },
  },
  // ...
});

Contribution

Feel free to fork and make a Pull Request to this plugin project. All the input is warmly welcome!