媒体库提供程序
¥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/
目录。如果你想将文件上传到其他位置,可以使用其他提供者。
¥By default Strapi provides a provider that uploads files to a local public/uploads/
directory in your Strapi project. Additional providers are available should you want to upload your files to another location.
Strapi 维护的提供商如下。单击卡片将重定向到他们的 Strapi 市场或 npm 页面:
¥The providers maintained by Strapi are the following. Clicking on a card will redirect you to their Strapi Marketplace or npm page:
Amazon S3
Official provider for file uploads to Amazon S3.
Cloudinary
Official provider for media management with Cloudinary.
Local
Default provider for storing files locally on the server.
提供者为插件的核心功能添加了扩展,例如将媒体文件上传到 AWS S3 而不是本地服务器,或者使用 Amazon SES 代替 Sendmail 发送电子邮件。
¥Providers add an extension to the core capabilities of the plugin, for example to upload media files to AWS S3 instead of the local server, or using Amazon SES for emails instead of Sendmail.
Strapi 维护的官方提供程序(可通过 市场 找到)以及许多社区维护的提供程序(可通过 npm 获取)。
¥There are both official providers maintained by Strapi — discoverable via the Marketplace — and many community maintained providers available via npm.
可以将提供程序配置为 private,以确保对资源 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
安装新的提供程序。
¥New providers can be installed using npm
or yarn
using the following format @strapi/provider-<plugin>-<provider> --save
.
例如,要为上传(媒体库)功能安装 AWS S3 提供程序:
¥For example, to install the AWS S3 provider for the Upload (Media Library) feature::
- Yarn
- NPM
yarn add @strapi/provider-upload-aws-s3
npm install @strapi/provider-upload-aws-s3 --save
配置提供者
¥Configuring providers
新安装的提供程序在 /config/plugins
文件 中启用并配置。如果该文件不存在,则必须创建它。
¥Newly installed providers are enabled and configured in the /config/plugins
file. If this file does not exist you must create it.
每个提供者都有不同的可用配置设置。查看 市场 或 npm 中该提供商的相应条目以了解更多信息。
¥Each provider will have different configuration settings available. Review the respective entry for that provider in the Marketplace or npm to learn more.
以下是 AWS S3 媒体库提供商的示例配置:
¥The following is an example configuration for an AWS S3 Media Library provider:
- JavaScript
- TypeScript
module.exports = ({ env }) => ({
// ...
upload: {
config: {
provider: 'aws-s3',
providerOptions: {
baseUrl: env('CDN_URL'),
rootPath: env('CDN_ROOT_PATH'),
s3Options: {
credentials: {
accessKeyId: env('AWS_ACCESS_KEY_ID'),
secretAccessKey: env('AWS_ACCESS_SECRET'),
},
region: env('AWS_REGION'),
params: {
ACL: env('AWS_ACL', 'public-read'),
signedUrlExpires: env('AWS_SIGNED_URL_EXPIRES', 15 * 60),
Bucket: env('AWS_BUCKET'),
},
},
},
actionOptions: {
upload: {},
uploadStream: {},
delete: {},
},
},
},
// ...
});
export default ({ env }) => ({
// ...
upload: {
config: {
provider: 'aws-s3', // For community providers pass the full package name (e.g. provider: 'strapi-provider-upload-google-cloud-storage')
providerOptions: {
accessKeyId: env('AWS_ACCESS_KEY_ID'),
secretAccessKey: env('AWS_ACCESS_SECRET'),
region: env('AWS_REGION'),
params: {
ACL: env('AWS_ACL', 'public-read'), // 'private' if you want to make the uploaded files private
Bucket: env('AWS_BUCKET'),
},
},
},
},
// ...
});
-
Strapi 具有默认
security
中间件,其具有非常严格的contentSecurityPolicy
,将图片和媒体的加载限制为仅"'self'"
,有关更多信息,请参阅 提供者页面 或 中间件文档 上的示例配置。¥Strapi has a default
security
middleware that has a very strictcontentSecurityPolicy
that limits loading images and media to"'self'"
only, see the example configuration on the provider page or the middleware documentation for more information. -
当每个环境使用不同的提供程序时,请在
/config/env/${yourEnvironment}/plugins.js|ts
中指定正确的配置(请参阅 环境)。¥When using a different provider per environment, specify the correct configuration in
/config/env/${yourEnvironment}/plugins.js|ts
(See Environments). -
一次只有一个电子邮件提供者处于活动状态。如果 Strapi 未选择电子邮件提供者设置,请验证
plugins.js|ts
文件是否位于正确的文件夹中。¥Only one email provider will be active at a time. If the email provider setting isn't picked up by Strapi, verify the
plugins.js|ts
file is in the correct folder. -
使用在 Strapi 设置期间创建的两个电子邮件模板测试新电子邮件提供者时,模板上的发送者电子邮件默认为
no-reply@strapi.io
,需要根据你的电子邮件提供者进行更新,否则将无法通过测试(请参阅 本地配置模板)。¥When testing the new email provider with those two email templates created during strapi setup, the shipper email on the template defaults to
no-reply@strapi.io
and needs to be updated according to your email provider, otherwise it will fail the test (See Configure templates locally).