mhunesi/yii2-storage

为 Yii2 的文件存储抽象层

安装次数: 21

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

v1.0.0 2023-01-17 06:26 UTC

This package is auto-updated.

Last update: 2024-09-26 15:19:24 UTC


README

为 Yii2 提供文件存储组件

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一

php composer.phar require --prefer-dist mhunesi/yii2-storage "*"

或在您的 composer.json 文件的 require 部分添加

"mhunesi/yii2-storage": "*"

使用方法

一旦安装了扩展,只需在您的代码中使用它即可

'components' => [
    //...
    'storage' => [
        'class' => '\mhunesi\storage\Storage',
        'defaultStorage' => 'local',
        'queueFilters' => true,
        'queueFiltersList' => ['tiny-crop', 'medium-thumbnail'],
        'downloadValidationKey' => 'YII_STORAGE_KEY',
        'downloadUrl' => '/site/download',
        'storages' => [
            'local' => [
                'class' => '\mhunesi\storage\filesystem\LocalFilesystem',
                'path' => '@app/web/uploads',
                'cache' => 'cache',
                'cacheKey' => 'filesystem_file',
                //'replica' => 'minio',
                'replica' => [
                    'class' => '\mhunesi\storage\filesystem\LocalFilesystem',
                    'path' => '@app/web/uploads2',
                    'cache' => 'cache',
                    'cacheKey' => 'filesystem_file_2',
                ]
            ],
            'aws' => [
                'class' => 'mhunesi\storage\filesystem\AwsS3Filesystem',
                'bucket' => 'bucket_name',
                'key' => '_key',
                'secret' => 'secret_key',
                'region' => 'eu-central-1',
                // 'version' => 'latest',
                'prefix' => 'wss',
                'cache' => 'cache',
                'cacheKey' => 'filesystem_aws',
                'replica' => [
                    'class' => '\mhunesi\storage\filesystem\LocalFilesystem',
                    'path' => '@app/web/uploads2',
                    'cache' => 'cache',
                    'cacheKey' => 'filesystem_file_2',
                ],
            ],
            'minio' => [
                'class' => 'mhunesi\storage\filesystem\AwsS3Filesystem',
                'bucket' => 'bucket_name',
                'key' => '_key',
                'secret' => 'scret_key',
                'region' => 'eu-central-1',
                // 'version' => 'latest',
                // 'baseUrl' => 'your-base-url',
                'prefix' => 'subfolder',
                // 'options' => [],
                'endpoint' => 'http://127.0.0.1:49160',
                'cache' => 'cache',
                'cacheKey' => 'filesystem_minio'
            ],
            'ftp' => [
                'class' => 'mhunesi\storage\filesystem\FtpFilesystem',
                'host' => '192.168.1.1',
                'port' => 21,
                'username' => 'ftp_username',
                'password' => 'ftp_password',
                'cache' => 'cache',
                'cacheKey' => 'filesystem_ftp1',
                // 'ssl' => true,
                // 'timeout' => 60,
                'root' => '/rootPath',
                'publicUrl' => 'http://ftp_url/',
                // 'permPrivate' => 0700,
                // 'permPublic' => 0744,
                // 'passive' => false,
                // 'transferMode' => FTP_TEXT,
                'replica' => 'local',
            ],
        ]
    ]    
      
    //...
],
'controllerMap' => [
    'migrate' => [
            //..
        'migrationNamespaces' => [
            //..
        ],
        'migrationPath' => [
            //..
            '@mhunesi/storage/migrations'
        ]
    ]
],

上传 & 下载操作

/**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'download' => [
                'class' => 'mhunesi\storage\actions\DownloadAction',
            ],
            'upload' => [
                'class' => 'mhunesi\storage\actions\FileUploadAction',
                'use_strict' => true,
                'path' => 'folder/folder',
                'folder' => 1,
                'hidden' => true,
                'visibility' => AdapterInterface::VISIBILITY_PUBLIC 
            ],
        ];
    }

URL 规则

'rules' => [
    'download/<token:[a-zA-Z0-9_\-\+\%\/=]*>/<filename:[a-zA-Z0-9_\-\+\%\.\ \/=]*>' => 'site/download'
]