mdhesari / laravel-flysystem-cloudinary
Cloudinary Flysystem v1与Laravel集成
Requires
- php: ^8.0
- cloudinary/cloudinary_php: ^2.2.0
- friendsofphp/php-cs-fixer: 3.0.0
- guzzlehttp/guzzle: ^7.0.1
- illuminate/contracts: ^8.0
- spatie/laravel-package-tools: ^1.4.3
Requires (Dev)
- brianium/paratest: ^6.2
- nunomaduro/collision: ^5.3
- orchestra/testbench: ^6.15
- phpunit/phpunit: ^9.3
- spatie/laravel-ray: ^1.18.0
- vimeo/psalm: ^4.4
README
💡 什么是Cloudinary?
Cloudinary基本上是一种存储和提供资产(如图像或视频)的绝佳方式。您可以将全分辨率图像上传到Cloudinary,他们为您处理优化。您唯一需要做的就是向您的URL添加额外的参数 😉
🛠 要求
- PHP:
^8.0
- Laravel:
^8.12
- league/flysystem:
^1.1
(Laravel 8默认) - Cloudinary账户
⚙️ 安装
您可以通过composer安装此包
composer require codebar-ag/laravel-flysystem-cloudinary
将以下磁盘添加到您的filesystems.php
配置中的"disks"列表中
'disks' => [ // 'cloudinary' => [ 'driver' => 'cloudinary', 'cloud_name' => env('CLOUDINARY_CLOUD_NAME'), 'api_key' => env('CLOUDINARY_API_KEY'), 'api_secret' => env('CLOUDINARY_API_SECRET'), 'url' => [ 'secure' => (bool) env('CLOUDINARY_SECURE_URL', true), ], ], ],
将以下环境变量添加到您的.env
文件中
FILESYSTEM_DRIVER=cloudinary CLOUDINARY_CLOUD_NAME=my-cloud-name CLOUDINARY_API_KEY=my-api-key CLOUDINARY_API_SECRET=my-api-secret
🏗 文件扩展名问题
让我们看看以下示例
use Illuminate\Support\Facades\Storage; Storage::disk('cloudinary')->put('cat.jpg', $contents);
这将生成以下具有双扩展名的URL
https://res.cloudinary.com/my-cloud-name/image/upload/v1/cat.jpg.jpg
为了防止这种情况,您应该在没有文件扩展名的情况下存储您的图像
use Illuminate\Support\Facades\Storage; Storage::disk('cloudinary')->put('cat', $contents);
现在这要好得多
https://res.cloudinary.com/my-cloud-name/image/upload/v1/cat.jpg
🪐 如何与Nova一起使用
要自定义存储文件的名称,您可以使用Image
字段的storeAs
方法
use Illuminate\Http\Request; use Laravel\Nova\Fields\Image; Image::make('Image') ->disk('cloudinary') ->storeAs(function (Request $request) { return sha1($request->image->getClientOriginalName()); }),
🗂 如何使用文件夹前缀
想象以下示例。我们有不同的客户,但希望在同一个Cloudinary账户中存储资产。通常,我们必须在每个路径前加上正确的客户文件夹名称。幸运的是,该包在这里帮助我们。我们可以在环境文件中配置一个文件夹,如下所示
CLOUDINARY_FOLDER=client_cat
现在,所有我们的资产都将带有client_cat/
文件夹的前缀。当我们存储以下图像时
use Illuminate\Support\Facades\Storage; Storage::disk('cloudinary')->put('meow', $contents);
它将生成以下URL
https://res.cloudinary.com/my-cloud-name/image/upload/v1/client_cat/meow.jpg
在媒体库中,它存储在client_cat/meow
中,并且您可以使用meow
检索图像
use Illuminate\Support\Facades\Storage;
Storage::disk('cloudinary')->getUrl('meow');
这应该会增加存储和检索您资产的正确文件夹的信任度。
🔋 速率限制注意事项
Cloudinary中的所有文件都存储为资源类型。它有三种类型:image
、raw
和video
。例如,如果我们想检查视频是否存在,我们需要最多发出3个请求。每种类型都需要单独检查,并发出单独的请求。
请注意,因为管理员API每小时限制为500次调用。
该包按照以下顺序进行检查
image
➡️raw
➡️video
🔧 配置文件
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="flysystem-cloudinary-config"
这是发布配置文件的内容
<?php return [ /* |-------------------------------------------------------------------------- | Cloudinary Upload Preset |-------------------------------------------------------------------------- | | Upload preset allow you to define the default behavior for all your | assets. They have precedence over client-side upload parameters. | You can define your upload preset in your cloudinary settings. | */ 'upload_preset' => env('CLOUDINARY_UPLOAD_PRESET'), /* |-------------------------------------------------------------------------- | Cloudinary Folder |-------------------------------------------------------------------------- | | An optional folder name where the uploaded asset will be stored. The | public ID contains the full path of the uploaded asset, including | the folder name. This is very useful to prefix assets directly. | */ 'folder' => env('CLOUDINARY_FOLDER'), /* |-------------------------------------------------------------------------- | Cloudinary Secure URL |-------------------------------------------------------------------------- | | This value determines that the asset delivery is forced to use HTTPS | URLs. If disabled all your assets will be delivered as HTTP URLs. | Please do not use unsecure URLs in your production application. | */ 'secure_url' => (bool) env('CLOUDINARY_SECURE_URL', true), ];
🚧 测试
运行测试
composer test
📝 变更日志
请参阅变更日志了解最近的变化信息。
✏️ 贡献
请参阅贡献指南获取详细信息。
🧑💻 安全漏洞
请查阅我们的安全策略了解如何报告安全漏洞。
🙏鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。