wearemodus/flysystem-cloudinary
Composer 安装命令:
composer require wearemodus/flysystem-cloudinary
包简介
Cloudinary adapter for Flysystem.
README 文档
README
This is a Flysystem adapter for Cloudinary API.
Installation
composer require wearemodus/flysystem-cloudinary '~2.0'
Or just add the following string to require part of your composer.json:
{
"require": {
"wearemodus/flysystem-cloudinary": "~2.0"
}
}
Bootstrap
<?php use WeAreModus\Flysystem\Cloudinary\ApiFacade as CloudinaryClient; use WeAreModus\Flysystem\Cloudinary\CloudinaryAdapter; use League\Flysystem\Filesystem; include __DIR__ . '/vendor/autoload.php'; $client = new CloudinaryClient([ 'cloud_name' => 'your-cloudname-here', 'api_key' => 'api-key', 'api_secret' => 'You-know-what-to-do', 'overwrite' => true, // set this to true if you want to overwrite existing files using $filesystem->write(); ]); $adapter = new CloudinaryAdapter($client); // This option disables assert that file is absent before calling `write`. // It is necessary if you want to overwrite files on `write` as Cloudinary does it by default. $filesystem = new Filesystem($adapter, ['disable_asserts' => true]);
Cloudinary features
Please, keep in mind three possible pain-in-asses of Cloudinary:
- It adds automatically file extension to its public_id. In terms of Flysystem, cloudinary's public_id is considered as filename. However, if you set public_id as 'test.jpg' Cloudinary will save the file as 'test.jpg.jpg'. In order to work it around, you can use PathConverterInterface.
- It does not support folders creation through the API
- If you want to save your files using folder you should set public_ids like 'test/test.jpg' and allow automated folder creation in your account settings in Cloudinary dashboard.
Good news!
The library supports Cloudinary Transformations!
WeAreModus Fork
This fork includes some essential changes needed for my use-case. See CHANGELOG.md for a full list of changes, but here are the cliff notes:
- Upload settings default to resource_type 'auto' in order to support video upload in addition to images
- DataUri now reads the mime-type of media using only the first 1 MB, in order to prevent memory overflow for large files due to
finfo->buffer() - ApiFacade constructor now accepts an uploadOptions argument containing options that are passed through to the Cloudinary SDK's upload function
- Added explicit PHP types (this will prevent compatibility with PHP
<7.4) - Updated dependencies (PHP >=7.4, PHPUnit ^9, cloudinary_php ^2, php_codesniffer 3.*)
Using with Laravel-MediaLibrary
In order to integrate this Cloudinary filesystem with Laravel-MediaLibrary, you will need to create a new filesystem driver as outline below:
- Define your Cloudinary
.envvariablesCLOUDINARY_CLOUD_NAME=CLOUDINARY_API_KEY=CLOUDINARY_API_SECRET=
- Define a new filesystem driver in your
filesystems.phpconfig file (within the 'disks' array)'disks' => [ ... 'cloudinary' => [ 'driver' => 'cloudinary', 'cloud_name' => env('CLOUDINARY_CLOUD_NAME'), 'api_key' => env('CLOUDINARY_API_KEY'), 'api_secret' => env('CLOUDINARY_API_SECRET'), ], ]
- Create a new CloudinaryServiceProvider
php artisan make:provider CloudinarySeviceProvider, and insert this code into theboot()methoduse WeAreModus\Flysystem\Cloudinary\ApiFacade as CloudinaryClient; use WeAreModus\Flysystem\Cloudinary\CloudinaryAdapter; use WeAreModus\Flysystem\Cloudinary\Converter\TruncateExtensionConverter; use Illuminate\Support\Facades\Storage; use Illuminate\Support\ServiceProvider; use League\Flysystem\Filesystem; ... public function boot() { Storage::extend('cloudinary', function ($app, $config) { $client = new CloudinaryClient( [ 'cloud_name' => $config['cloud_name'], 'api_key' => $config['api_key'], 'api_secret' => $config['api_secret'], 'overwrite' => $config['overwrite'] ?? true, ], // Cloudinary API options [ 'resource_type' => 'auto', 'eager' => [ ['fetch_format' => 'mp4', 'format' => '', 'video_codec' => 'h264', 'quality' => '70'], // f_mp4,vc_h264,q_70 ['fetch_format' => 'png', 'format' => '', 'quality' => '70'], // f_png,q_70 ], 'eager_async' => 'true', 'eager_notification_url' => 'https://mysite.test/webhook/eager', 'notification_url' => 'https://mysite.test/webhook/upload', ... ], // Upload options new TruncateExtensionConverter() ); return new Filesystem(new CloudinaryAdapter($client)); }); }
- Finally, simply define
'cloudinary'as your diskName when managing media$this->addMediaCollection('videos')->useDisk('cloudinary'); // Use Cloudinary as disk for the entire collection // OR $model->addMedia($mediaPath) ->toMediaCollection('videos', 'cloudinary'); // USe Cloudinary as disk for this media only
wearemodus/flysystem-cloudinary 适用场景与选型建议
wearemodus/flysystem-cloudinary 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 870 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 07 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wearemodus/flysystem-cloudinary 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wearemodus/flysystem-cloudinary 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 870
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-22