ccasanovas/cake-s3upload

CakePHP 的 S3UploadSDK 插件

1.0.4 2023-02-04 23:46 UTC

This package is auto-updated.

Last update: 2024-09-05 03:11:46 UTC


README

此插件允许通过提供的 behavior,快速将 Table 中的文件上传到 AWS S3 的 bucket。

变更日志

日期:2020-11-24

  • 修复了 behavior 中的已弃用警告

日期:2019-03-25

  • 修复了导致文件作为纯文本上传到 S3 的错误

日期:2018-11-20

  • 添加了当文件在相同表格中时删除文件的功能

日期:2018-10-11

  • 重写了文档

文档

功能

实现

  • 包括以下功能的 behavior,用于上传文件的表格,S3UploadSDK:
    • 上传文件到配置的 s3 bucket
    • 在数据库中存储元数据字段以及文件的完整 URL。
    • 允许轻松配置要上传的文件为仅图像
    • 提供预配置的多个验证器,以便快速集成
    • 允许切换以本地上传文件
    • 删除表格中的文件的方法,而不会删除完整的行。

待办事项

  • 允许同时配置多个 bucket

安装

要使用 composer 获取插件,需要在 composer.json 中添加以下内容:

  1. "require" 对象中添加插件: "ccasanovas/cake-s3upload": "dev-master"
  2. "repositories" 数组中添加对象:{"type": "vcs", "url": "git@bitbucket.org:ccasanovas/cake-s3upload.git"}
  3. 运行 composer update

注意:确保在存储库中拥有正确的访问/部署权限。

一旦在存储库中安装了插件,就可以将 behavior 添加到所需的表格中

$this->loadBehavior('Ccasanovas/S3UploadSDK.S3UploadSDK', [
    /* Nombre del campo principal, donde se guardará el nombre del archivo: */
    'avatar_file_name' => [
        /* Estos campos se almacenarán en la base de datos: */
        'fields' => [
            /* campos requeridos: */
            'dir'  => 'avatar_file_dir', //el directorio del archivo
            'size' => 'avatar_file_size', //el tamaño del archivo en bytes
            'type' => 'avatar_file_type', //el mime type del archivo
            'url'  => 'avatar_url', //la dirección completa del archivo una vez subido

            /* campos opcionales usados para imagenes: */
            'image_width'  => 'avatar_width', //ancho de la imagen
            'image_height' => 'avatar_height' //alto de la imagen
        ],
        /*  si el campo se configura como images_only,
            se agregará validación para asegurar que solo se suban imagenes
        */
        'images_only' => true
    ]
]);

结构

Ccasanovas\S3UploadSDK\Model\Behavior\AwsS3UploadBehavior

  • public initialize(array $config)
    • 检查必要的配置变量是否存在且正确
    • 构建并配置客户端以与 S3 交互
    • 为所有在模型中配置的字段添加 s3 适配器和默认路径
    • 在处理完字段的配置后,向 Table 添加 behavior 'Josegonzalez/Upload.Upload' 并传递它们
  • public buildValidator(Event $event, Validator $validator, $name)
    • 为每个配置的字段添加标准验证规则
    • 包括以下规则
      • fileUnderPhpSizeLimit
      • fileUnderFormSizeLimit
      • fileCompletedUpload
      • fileFileUpload
      • fileSuccessfulWrite
    • 此外,如果字段配置为 ['images_only' => true],则添加对 mimeType 的验证,仅接受以下类型
      • image/gif
      • image/png
      • image/jpg
      • image/jpeg
  • public afterRules(Event $event, EntityInterface $entity, ArrayObject $options, $result, $operation)
    • 如果配置了高宽字段,则尝试从文件中获取这些属性并填充这些字段
  • public beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)
    • 将上传到 S3 的文件的完整 URL 存储在 url 字段中
    • 如果正在删除文件,则负责清理相关的字段(dir、type、size 等)
  • public deleteFile($id, $field)
    • 删除实体中的 $field 的实用方法。与 beforeSave 一起,可以调用例如 $this->Products->deleteFile($product_id, 'file_name');,这将删除 file_name 的内容及其相关字段(例如:file_dir、file_size、file_type 等)。

配置变量

配置变量与应用程序的配置数组一起保存,就像其他配置一样(默认为 config/app.php)。

可用的配置有

'AwsS3' => [
    'base_url' => 'https://s3.amazonaws.com',
    'key'      => '', //required.
    'secret'   => '', //required.
    'region'   => 'us-east-1', //required. region the bucket is it
    'bucket'   => 'test-bucket-name', //required. name of the bucket
    'prefix'   => 'path/to/test/prefix' //optional. Appends this path to all s3 object address.

    /*  Campo opcional. Por defecto false. se puede poner explicitamente
        como true para que no se use s3 y en su lugar se almacene localmente.
        Util para testing.
    */
    'local_only' => false,
]

测试

//TODO