更新:我安装了 Strapi 版本 3.6.3 并且运行良好
Strapi - Clouinary 连接对我不起作用。所以我将图片上传到 Stapi,但它们没有出现在 Clouinary 中。
在配置文件夹中,我创建了包含以下内容的文件 plugins.js:
module.exports = ({
env
}) => ({
// ...
upload: {
provider: 'cloudinary',
providerOptions: {
cloud_name: env('CLOUDINARY_NAME'),
api_key: env('CLOUDINARY_KEY'),
api_secret: env('CLOUDINARY_SECRET'),
},
},
// ...
});
我已经安装了
npm i strapi-provider-upload-cloudinary
然后将文件 .env 更改为
PORT=1337
CLOUDINARY_NAME="***"
CLOUDINARY_KEY="***"
CLOUDINARY_SECRET="***"```
Actually automatically following code added automatically:
```JWT_SECRET=*********
API_TOKEN_SALT=*********
JWT_SECRET=*********
可能出现什么问题? CLOUDINARY_SECRET 应该在“引号”中吗?或者在“引号”中或不带引号?
添加图像后的终端输出如下:
http://localhost:1337
[2021-12-07 02:10:14.702] http: POST /upload (261 ms) 200
[2021-12-07 02:10:14.744] http: GET /upload/files?sort=updatedAt:DESC&page=1&pageSize=10 (24 ms) 200
[2021-12-07 02:10:14.758] http: GET /uploads/thumbnail_Screenshot_2021_11_26_130226_11a95e81ea.png?width=1504&height=1258 (4 ms) 200
所有权限似乎都已设置...
还创建了extents/upload/config/setting.json,内容如下:
"provider": "cloudinary",
"providerOptions": { "cloud_name":"devert0mt",
"api_key": "***",
"api_secret":"***"
}
}{
"provider": "cloudinary",
"providerOptions": { "cloud_name":"devert0mt",
"api_key": "***",
"api_secret":"***"
}
}```
如果您想使用最新版本的 Strapi,v.4 及以上版本,您需要将 Strapi 提供程序包更改为此:
npm install @strapi/provider-upload-cloudinary --save
然后,您需要将
plugins.js
中的 config/plugins.js
文件更新为以下内容(请注意,它的结构与之前的包略有不同 - 所有内容都放在 config 对象中,而不是像它那样的 upload 中)在以前的版本上):
module.exports = ({ env }) => ({
// ...
upload: {
config: {
provider: 'cloudinary',
providerOptions: {
cloud_name: env('CLOUDINARY_NAME'),
api_key: env('CLOUDINARY_KEY'),
api_secret: env('CLOUDINARY_SECRET'),
},
actionOptions: {
upload: {},
delete: {},
},
},
},
// ...
});
此外,如果您在 Strapi 仪表板上正确渲染图像时遇到问题,您可以在
middlewares.js
中更新 config/middlewares.js
:
粘贴以下内容,而不是
'strapi::security'
中的 module.exports
:
// ...
{
name: 'strapi::security',
config: {
contentSecurityPolicy: {
useDefaults: true,
directives: {
'connect-src': ["'self'", 'https:'],
'img-src': ["'self'", 'data:', 'blob:', 'res.cloudinary.com'],
'media-src': ["'self'", 'data:', 'blob:', 'res.cloudinary.com'],
upgradeInsecureRequests: null,
},
},
},
},
// ...
如果您在最新版本中完成了所有配置但仍不起作用,也许这就是解决方案。我检查文件名是
config/plugins.js
而不是config/plugin.js
。该文件必须有 s
。哈哈