codebar-ag / laravel-flysystem-cloudinary
Laravel 与 Cloudinary Flysystem v1 集成
Requires
- php: >=8.2
- cloudinary/cloudinary_php: ^2.13
- guzzlehttp/guzzle: ^7.8
- illuminate/contracts: ^11.0
- nesbot/carbon: ^2.72
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1
- orchestra/testbench: ^9.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.35
README
💡 什么是 Cloudinary?
Cloudinary 是一种存储和提供如图片或视频等资源的绝佳方式。您可以将全分辨率图片上传到 Cloudinary,他们负责为您进行优化。您需要做的就是向 URL 添加额外的参数 😉
🛠 要求
- 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 一起使用
我们有一个用于 Laravel Nova 的包:Laravel Flysystem Cloudinary Nova
🗂 如何使用文件夹前缀
想象以下示例。我们有不同的客户,但想将资源存储在同一 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
⚙️ 可选参数
Cloudinary 有许多可选参数可以自定义上传。您可以在 官方文档 的可选参数部分找到所有选项。
您可以将所有参数作为数组传递给 put
方法
use Illuminate\Support\Facades\Storage; Storage::disk('cloudinary')->put('meow', $contents, [ 'options' [ 'notification_url' => 'https://mysite.example.com/notify_endpoint', 'async' => true, ] ]);
注意:如果您发现自己正在为所有请求使用相同的参数,您应该考虑将它们添加到配置文件中。(见下文)
🔧 配置文件
您可以使用以下命令发布配置文件
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), /* |-------------------------------------------------------------------------- | Cloudinary Global Upload Options |-------------------------------------------------------------------------- | | Here you may specify the upload options that will be applied to all | your assets. This will be merged with the options that you may | define in the `Storage::disk('cloudinary')` call. | */ 'options' => [ // 'async' => true, ], ];
🚧 测试
运行测试
composer test
📝 变更日志
请参阅 CHANGELOG 以获取有关最近更改的更多信息。
✏️ 贡献
请参阅 CONTRIBUTING 了解详细信息。
🧑💻 安全漏洞
请查看我们的安全策略,了解如何报告安全漏洞。
🙏 致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。