Skip to main content

媒体库提供商

🌐 Media Library providers

媒体库 功能由一个名为 Upload 的后端服务器包提供支持,该包利用了提供程序的使用。

🌐 The Media Library feature is powered by a back-end server package called Upload which leverages the use of providers.

默认情况下,Strapi 提供了一个提供程序,可以将文件上传到 Strapi 项目中的本地 public/uploads/ 目录。还可以使用其他提供程序,这些提供程序为插件的核心功能添加了扩展。使用它们可以将文件上传到其他位置,例如 AWS S3 或 Cloudinary。

🌐 By default Strapi provides a provider that uploads files to a local public/uploads/ directory in your Strapi project. Additional providers are available and add an extension to the core capabilities of the plugin. Use them to upload your files to another location, such as AWS S3 or Cloudinary.

Strapi 维护官方提供者,可通过 Marketplace 发现。社区维护的提供者也可以通过 npm 获取。

可以将提供者配置为私有,以确保资源 URL 会被签名以实现安全访问。

🌐 A provider can be configured to be private to ensure asset URLs will be signed for secure access.

安装提供程序

🌐 Installing providers

可以使用 NPM 或 Yarn 安装新提供者,使用以下格式 @strapi/provider-<plugin>-<provider> --save

例如,要为媒体库功能安装 AWS S3 提供程序:

🌐 For example, to install the AWS S3 provider for the Media Library feature:

yarn add @strapi/provider-upload-aws-s3

配置提供者

🌐 Configuring providers

新安装的提供程序已在 “/config/plugins” 文件 中启用并配置。如果该文件不存在,请先创建它。

🌐 Newly installed providers are enabled and configured in the /config/plugins file. If this file does not exist, create it first.

每个提供者将有不同的配置设置可用。请查看下方 Strapi 维护的提供者的专用页面中该提供者的相关条目,或在提供者的 Marketplace 页面上查看。

Strapi 维护的提供者如下。点击卡片将重定向到带有配置示例的文档页面:

🌐 The providers maintained by Strapi are the following. Clicking on a card will redirect you to a docs page with a configuration example:

Notes
  • Strapi 有一个默认的 security 中间件,它有一个非常严格的 contentSecurityPolicy,只允许加载 "'self'" 的图片和媒体。有关更多信息,请参见 provider page 上的示例配置或 中间件文档
  • 在每个环境使用不同的提供者时,请在 /config/env/${yourEnvironment}/plugins.js|ts 中指定正确的配置(参见 environments)。

每个环境的配置

🌐 Configuration per environment

在配置你的提供者时,你可能希望根据 NODE_ENV 环境变量更改配置,或使用特定环境的凭据。

🌐 When configuring your provider you might want to change the configuration based on the NODE_ENV environment variable or use environment specific credentials.

你可以在 /config/env/{env}/plugins.js|ts 配置文件中设置特定配置,它将用于覆盖默认配置。

🌐 You can set a specific configuration in the /config/env/{env}/plugins.js|ts configuration file and it will be used to overwrite the default configuration.

创建提供者

🌐 Creating providers

要实现你自己的自定义提供程序,你必须 create a Node.js module

必须导出的接口取决于你正在为其开发提供程序的插件。以下是上传(媒体库)和电子邮件功能的模板:

🌐 The interface that must be exported depends on the plugin you are developing the provider for. The following are templates for the Upload (Media Library) and Email features:

module.exports = {
init(providerOptions) {
// init your provider if necessary

return {
upload(file) {
// upload the file in the provider
// file content is accessible by `file.buffer`
},
uploadStream(file) {
// upload the file in the provider
// file content is accessible by `file.stream`
},
delete(file) {
// delete the file in the provider
},
checkFileSize(file, { sizeLimit }) {
// (optional)
// implement your own file size limit logic
},
getSignedUrl(file) {
// (optional)
// Generate a signed URL for the given file.
// The signed URL allows secure access to the file.
// Only Content Manager assets will be signed.
// Returns an object {url: string}.
},
isPrivate() {
// (optional)
// if it is private, file urls will be signed
// Returns a boolean
},
};
},
};

在发送功能中,你将可以访问:

🌐 In the send function you will have access to:

  • providerOptions 包含用 plugins.js|ts 编写的配置
  • settings 包含用 plugins.js|ts 编写的配置
  • options 包含在你从电子邮件插件服务调用发送函数时发送的选项

你可以查看 Strapi-maintained providers 以获取示例实现。

在创建新的提供者后,你可以 publish it to npm 与社区分享,或仅在你的项目中本地使用

本地供应商

🌐 Local providers

如果你想创建自己的提供程序而不将其发布到 npm 上,你可以按照以下步骤操作:

🌐 If you want to create your own provider without publishing it on npm you can follow these steps:

  1. 在你的应用中创建一个 providers 文件夹。
  2. 创建你的提供商(例如 /providers/strapi-provider-<plugin>-<provider>
  3. 然后更新你的 package.json,将你的 strapi-provider-<plugin>-<provider> 依赖链接到你新提供者的 local path
{
...
"dependencies": {
...
"strapi-provider-<plugin>-<provider>": "file:providers/strapi-provider-<plugin>-<provider>",
...
}
}
  1. 更新你的 /config/plugins.js|ts 文件以 配置提供者
  2. 最后,运行 yarnnpm install 来安装你新的自定义提供程序。

私有供应商

🌐 Private providers

你可以设置私有提供者,这意味着内容管理器中显示的每个资源 URL 都将经过签名以进行安全访问。

🌐 You can set up a private provider, meaning that every asset URL displayed in the Content Manager will be signed for secure access.

要启用私有提供者,你必须实现 isPrivate() 方法并返回 true

🌐 To enable private providers, you must implement the isPrivate() method and return true.

在后端,Strapi 使用提供者中实现的 getSignedUrl(file) 方法为每个资源生成一个签名 URL。签名 URL 包含一个加密签名,允许用户访问该资源(但通常仅在有限时间内且具有特定限制,取决于提供者)。

🌐 In the backend, Strapi generates a signed URL for each asset using the getSignedUrl(file) method implemented in the provider. The signed URL includes an encrypted signature that allows the user to access the asset (but normally only for a limited time and with specific restrictions, depending on the provider).

请注意,出于安全原因,内容 API 不会提供任何已签名的 URL。相反,使用该 API 的开发者应自己为 URL 签名。

🌐 Note that for security reasons, the content API will not provide any signed URLs. Instead, developers using the API should sign the urls themselves.

Private AWS S3 provider

你可以在专用文档中找到关于如何创建私有 Amazon AWS S3 提供程序的简短指南。

🌐 You can find a short guide on how to create a private Amazon AWS S3 provider in the dedicated documentation.