mdhesari/laravel-flysystem-cloudinary

此包已被废弃且不再维护。未建议替代包。

Cloudinary Flysystem v1与Laravel集成

1.0.3 2021-06-30 07:25 UTC

This package is auto-updated.

Last update: 2022-01-30 09:42:08 UTC


README

68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c253230466c7973797374656d253230436c6f7564696e6172792e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d636f64656261722d61672532466c61726176656c2d666c7973797374656d2d636c6f7564696e617279267061747465726e3d63697263756974426f617264267374796c653d7374796c655f32266465736372697074696f6e3d416e2b6f70696e696f6e617465642b7761792b746f2b696e746567726174652b436c6f7564696e6172792b776974682b7468652b4c61726176656c2b66696c6573797374656d266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313530707826696d616765733d636c6f7564267769647468733d35303026686569676874733d353030

Latest Version on Packagist Total Downloads run-tests Check & fix styling

💡 什么是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中的所有文件都存储为资源类型。它有三种类型:imagerawvideo。例如,如果我们想检查视频是否存在,我们需要最多发出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)。请参阅许可证文件获取更多信息。