媒体库
¥Media Library
媒体库是 Strapi 的功能,可显示 Strapi 应用中上传的所有资源并允许用户管理它们。
¥The Media Library is the Strapi feature that displays all assets uploaded in the Strapi application and allows users to manage them.
配置
¥Configuration
媒体库的一些配置选项在管理面板中可用,一些通过你的 Strapi 项目的代码处理。
¥Some configuration options for the Media Library are available in the admin panel, and some are handled via your Strapi project's code.
管理面板配置
¥Admin panel configuration
在管理面板中,可以通过全局设置使用一些媒体库设置来管理上传资源的格式、文件大小和方向。也可以直接通过媒体库配置视图。
¥In the admin panel, some Media Library settings are available via the Global Settings to manage the format, file size, and orientation of uploaded assets. It is also possible, directly via the Media Library to configure the view.
配置设置
¥Configuring settings
配置功能的路径: 设置 > 全局设置 > 媒体库。
¥Path to configure the feature: Settings > Global Settings > Media Library.
-
定义你选择的新媒体库设置:
¥Define your chosen new Media Library settings:
设置名称 指示 默认值 响应式友好上传 启用此选项将生成上传资源的多种格式(小、中、大)。
每种格式的默认大小可以是 通过代码配置。真的 尺寸优化 启用此选项将减小图片尺寸并略微降低其质量。 真的 自动定向 启用此选项将根据 EXIF 方向标签自动旋转图片。 错误的 -
单击“保存”按钮。
¥Click on the Save button.


配置视图
¥Configuring the view
配置功能的路径: 媒体库
¥Path to configure the feature: Media Library
-
单击界面右侧文件夹和资源列表上方的 按钮。
¥Click on the button just above the list of folders and assets, on the right side of the interface.
-
按照以下说明配置媒体库视图:
¥Configure the Media Library view, following the instructions below:
设置名称 指示 每页条目数 使用下拉菜单定义每页默认显示的资源数量。 默认排序顺序 使用下拉菜单定义资源显示的默认顺序。在对媒体库中的资源进行排序时可以覆盖此操作。
这两种设置都是媒体库和内容管理器媒体上传模式的默认设置。这些设置在整个 Strapi 项目中对所有用户都是全局的。
¥Both settings are used as the defaults in the Media Library and in the Content Manager's media upload modal. These settings are global across the entire Strapi project for all users.


基于代码的配置
¥Code-based configuration
媒体库由上传包在后端服务器中提供支持,可以通过提供程序进行配置和扩展。
¥The Media Library is powered in the backend server by the Upload package, which can be configured and extended through providers.
提供者
¥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.
如果你需要安装其他提供商或创建自己的提供商,请参考以下指南:
¥If you need to install other providers or create your own, please refer to the following guide:
当前页面上的基于代码的配置说明详细介绍了默认上传提供商的选项。如果使用其他提供商,请参阅该提供商文档中可用的配置参数。
¥Code-based configuration instructions on the present page detail options for the default upload provider. If using another provider, please refer to the available configuration parameters in that provider's documentation.
可用选项
¥Available options
使用默认上传提供程序时,可以在 config/plugins
文件 中的 upload.config
对象中声明以下特定配置选项。所有参数都是可选的:
¥When using the default upload provider, the following specific configuration options can be declared in an upload.config
object within the config/plugins
file. All parameters are optional:
范围 | 描述 | 类型 | 默认 |
---|---|---|---|
providerOptions.localServer | 将传递给 koa-static 的选项,在此基础上构建上传服务器(参见 本地服务器配置) | 目的 | * |
sizeLimit | 最大文件大小(以字节为单位)(参见 最大文件大小) | 整数 | 209715200 (200 MB,以字节为单位,即 200 x 1024 x 1024 字节) |
breakpoints | 允许在 "响应式友好上传" 选项设置为 true 时覆盖生成响应式图片的断点大小(参见 响应式图片) | 目的 | { large: 1000, medium: 750, small: 500 } |
Upload 请求超时在服务器选项中定义,而不是在 Upload 插件选项中定义,因为它不是特定于 Upload 插件的,而是应用于整个 Strapi 服务器实例(参见 上传请求超时)。
¥The Upload request timeout is defined in the server options, not in the Upload plugin options, as it's not specific to the Upload plugin but is applied to the whole Strapi server instance (see upload request timeout).
如果你希望覆盖图片函数以生成自定义文件名,请参阅 插件扩展 文档。
¥If you wish to override the image function to generate custom file names, please refer to the Plugins extension documentation.
自定义配置示例
¥Example custom configuration
以下是使用默认上传提供程序时上传插件的自定义配置示例:
¥The following is an example of a custom configuration for the Upload plugin when using the default upload provider:
- JavaScript
- TypeScript
module.exports = ({ env })=>({
upload: {
config: {
providerOptions: {
localServer: {
maxage: 300000
},
},
sizeLimit: 250 * 1024 * 1024, // 256mb in bytes
breakpoints: {
xlarge: 1920,
large: 1000,
medium: 750,
small: 500,
xsmall: 64
},
},
},
});
export default () => ({
upload: {
config: {
providerOptions: {
localServer: {
maxage: 300000
},
},
sizeLimit: 250 * 1024 * 1024, // 256mb in bytes
breakpoints: {
xlarge: 1920,
large: 1000,
medium: 750,
small: 500,
xsmall: 64
},
},
},
})
本地服务器
¥Local server
默认情况下,Strapi 接受本地上传文件的 localServer
配置。这些将作为 koa-static 的选项传递。
¥By default Strapi accepts localServer
configurations for locally uploaded files. These will be passed as the options for koa-static.
你可以通过创建或编辑 /config/plugins
文件 来提供它们。以下示例设置 max-age
标头:
¥You can provide them by creating or editing the /config/plugins
file. The following example sets the max-age
header:
- JavaScript
- TypeScript
module.exports = ({ env })=>({
upload: {
config: {
providerOptions: {
localServer: {
maxage: 300000
},
},
},
},
});
export default ({ env }) => ({
upload: {
config: {
providerOptions: {
localServer: {
maxage: 300000
},
},
},
},
});
最大文件大小
¥Max file size
负责解析请求的 Strapi 中间件需要配置为支持大于默认值 200MB 的文件大小。除了传递给 sizeLimit
的上传包的提供程序选项之外,还必须执行此操作。
¥The Strapi middleware in charge of parsing requests needs to be configured to support file sizes larger than the default of 200MB. This must be done in addition to provider options passed to the Upload package for sizeLimit
.
你可能还需要调整任何上游代理、负载平衡器或防火墙,以允许更大的文件大小。例如,Nginx 有一个名为 client_max_body_size
的配置设置必须进行调整,因为它的默认值只有 1mb。
¥You may also need to adjust any upstream proxies, load balancers, or firewalls to allow for larger file sizes. For instance, Nginx has a configuration setting called client_max_body_size
that must be adjusted, since its default is only 1mb.
上传包使用的中间件是 body
中间件。你可以通过在 /config/middlewares
文件中设置配置直接将配置传递给中间件:
¥The middleware used by the Upload package is the body
middleware. You can pass configuration to the middleware directly by setting it in the /config/middlewares
file:
- JavaScript
- TypeScript
module.exports = [
// ...
{
name: "strapi::body",
config: {
formLimit: "256mb", // modify form body
jsonLimit: "256mb", // modify JSON body
textLimit: "256mb", // modify text body
formidable: {
maxFileSize: 250 * 1024 * 1024, // multipart data, modify here limit of uploaded file size
},
},
},
// ...
];
export default [
// ...
{
name: "strapi::body",
config: {
formLimit: "256mb", // modify form body
jsonLimit: "256mb", // modify JSON body
textLimit: "256mb", // modify text body
formidable: {
maxFileSize: 250 * 1024 * 1024, // multipart data, modify here limit of uploaded file size
},
},
},
// ...
];
除了中间件配置外,你还可以在 /config/plugins 文件 中传递 sizeLimit
(以字节为单位的整数):
¥In addition to the middleware configuration, you can pass the sizeLimit
, which is an integer in bytes, in the /config/plugins file:
- JavaScript
- TypeScript
module.exports = {
// ...
upload: {
config: {
sizeLimit: 250 * 1024 * 1024 // 256mb in bytes
}
}
};
export default {
// ...
upload: {
config: {
sizeLimit: 250 * 1024 * 1024 // 256mb in bytes
}
}
};
上传请求超时
¥Upload request timeout
默认情况下,strapi.server.httpServer.requestTimeout
的值设置为 330 秒。这包括上传。
¥By default, the value of strapi.server.httpServer.requestTimeout
is set to 330 seconds. This includes uploads.
为了使互联网连接速度慢的用户能够上传大文件,可能需要增加此超时限制。建议的方法是设置 config/servers
文件 中的 http.serverOptions.requestTimeout
参数。
¥To make it possible for users with slow internet connection to upload large files, it might be required to increase this timeout limit. The recommended way to do it is by setting the http.serverOptions.requestTimeout
parameter in the config/servers
file.
另一种方法是在 Strapi 启动之前运行的 bootstrap
函数 中设置 requestTimeout
值。这在需要以编程方式更改的情况下很有用 - 例如,暂时禁用并重新启用它:
¥An alternate method is to set the requestTimeout
value in the bootstrap
function that runs before Strapi gets started. This is useful in cases where it needs to change programmatically—for example, to temporarily disable and re-enable it:
- JavaScript
- TypeScript
module.exports = {
//...
bootstrap({ strapi }) {
// Set the requestTimeout to 1,800,000 milliseconds (30 minutes):
strapi.server.httpServer.requestTimeout = 30 * 60 * 1000;
},
};
export default {
//...
bootstrap({ strapi }) {
// Set the requestTimeout to 1,800,000 milliseconds (30 minutes):
strapi.server.httpServer.requestTimeout = 30 * 60 * 1000;
},
};
响应式图片
¥Responsive Images
启用 Responsive friendly upload
管理面板设置 后,插件将生成以下响应式图片大小:
¥When the Responsive friendly upload
admin panel setting is enabled, the plugin will generate the following responsive image sizes:
名称 | 最大维度 |
---|---|
large | 1000 像素 |
medium | 750 像素 |
small | 500 像素 |
这些尺寸可以在 /config/plugins
中被覆盖:
¥These sizes can be overridden in /config/plugins
:
- JavaScript
- TypeScript
module.exports = ({ env }) => ({
upload: {
config: {
breakpoints: {
xlarge: 1920,
large: 1000,
medium: 750,
small: 500,
xsmall: 64
},
},
},
});
export default ({ env }) => ({
upload: {
config: {
breakpoints: {
xlarge: 1920,
large: 1000,
medium: 750,
small: 500,
xsmall: 64
},
},
},
});
断点更改仅适用于新图片,现有图片不会调整大小或生成新尺寸。
¥Breakpoint changes will only apply to new images, existing images will not be resized or have new sizes generated.
用法
¥Usage
使用功能的路径: 媒体库
¥Path to use the feature: Media Library
媒体库显示应用中上传的所有资源,可以通过 媒体库本身或通过 内容管理器在管理媒体字段时上传。
¥The Media Library displays all assets uploaded in the application, either via the Media Library itself or via the Content Manager when managing a media field.
上传到媒体库的资源可以使用 内容管理者 插入到内容类型中。
¥Assets uploaded to the Media Library can be inserted into content-types using the Content Manager.


从媒体库中,可以:
¥From the Media Library, it is possible to:
-
上传新资源(参见 添加资源)或创建新文件夹(参见 使用文件夹组织资源)1,
¥upload a new asset (see adding assets) or create a new folder (see organizing assets with folders) 1,
-
对资源和文件夹进行排序或设置过滤器 2 以更轻松地查找资源和文件夹,
¥sort the assets and folders or set filters 2 to find assets and folders more easily,
-
在列表视图 和网格视图 之间切换以显示资源,访问设置 至 配置视图,并进行文本搜索 3 以查找特定资源或文件夹,
¥toggle between the list view and the grid view to display assets, access settings to configure the view, and make a textual search 3 to find a specific asset or folder,
-
并查看、浏览和管理文件夹 4。
¥and view, navigate through, and manage folders 4.
单击用户界面右侧的搜索图标 以使用文本搜索并更快地找到你的资源或文件夹之一!
¥Click the search icon on the right side of the user interface to use a text search and find one of your assets or folders more quickly!
添加资源
¥Adding assets
List of media types and extensions supported by the Media Library
媒体类型 | 支持的扩展 |
---|---|
图片 | * JPEG - PNG - GIF - SVG - TIFF - ICO - DVU |
视频 | * MPEG - MP4 - MOV (Quicktime) - WMV - AVI - FLV |
声音的 | * MP3 - WAV - OGG |
文件 | * CSV - ZIP - XLS、XLSX - JSON |
-
单击媒体库右上角的添加新资源按钮。
¥Click the Add new assets button in the upper right corner of the Media Library.
-
选择是要从计 算机还是从 URL 上传新资源:
¥Choose whether you want to upload the new asset from your computer or from an URL:
-
从计算机上,直接拖放资源或浏览系统上的文件,
¥from the computer, either drag & drop the asset directly or browse files on your system,
-
从 URL 中,在 URL 字段中键入或复制并粘贴 URL,确保多个 URL 用回车符分隔,然后单击“下一步”。
¥from an URL, type or copy and paste an URL(s) in the URL field, making sure multiple URLs are separated by carriage returns, then click Next.
-
-
(可选)单击编辑按钮 查看资源元数据并为资源定义文件名、替代文本和标题(参见 管理个人资源)。
¥(optional) Click the edit button to view asset metadata and define a File name, Alternative text and a Caption for the asset (see Managing individual assets).
-
(可选)通过单击“添加新资源”并返回步骤 2 来添加更多资源。
¥(optional) Add more assets by clicking Add new assets and going back to step 2.
-
单击将资源上传到库。
¥Click on Upload assets to the library.

