/yii2-storage

Yii2 的资源(云)存储 SDK

1.0.4 2017-09-17 06:28 UTC

This package is not auto-updated.

Last update: 2024-09-24 17:28:38 UTC


README

此包是一个 ActiveRecord 行为。它允许您保持上传的文件原样。它支持多个属性和文件。

基于 Flysystem,支持 qiniuoss

安装

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

运行以下命令:

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

或者

"years/yii2-storage": "*"

将以下内容添加到您的 composer.json 文件的 require 部分:

配置

添加组件 'storage'

'components' => [
        'storage' => [
            'class' => 'years\storage\Storage',
        ],
   ]

将 'filesystems' 键添加到 'params.php'

return [
	 xxxxx
    'filesystems' => [
    	'default' => 'qiniu',
    	'disks' => [
        	'local' => [
            	'driver' => 'local',
            	'root' => '@webroot/upload',
        	],

        	's3' => [
            	'driver' => 's3',
            	'key' => 'your-key',
            	'secret' => 'your-secret',
            	'region' => 'your-region',
            	'bucket' => 'your-bucket',
        	],

        	'qiniu' => [
            	'driver'  => 'qiniu',
            	'domains' => [
                	'default'   => 'olys54us5.bkt.clouddn.com', //你的七牛域名
                	'https'     => '',         //你的HTTPS域名
                	'custom'    => '',                //你的自定义域名
            	],
            	'access_key'=> 'xxxxx-BCT2',  //AccessKey
            	'secret_key'=> 'xxxx',  //SecretKey
            	'bucket'    => 'xxx',  //Bucket名字
            	'notify_url'=> '',  //持久化处理回调地址
        	],

        	'oss' => [
                'driver'        => 'oss',
                'access_id'     => 'xxxx',
                'access_key'    => 'xxx',
                'bucket'        => 'xxx',
                'endpoint'      => 'oss-cn-hangzhou-internal.aliyuncs.com',
                'isCName'       => false,
                'debug'         => true
        	],
   	 	],
    ],
];


UploadBehavior

此行为允许您使用 ActiveRecord 行为添加文件上传逻辑。

用法

将行为附加到您的模型类

public function behaviors()
{
    return [
        [
            'class' => '\years\storage\behaviors\UploadBehavior',
            'attributes' => 'avatar',
            'disk' => 'qiniu',
            'directory' => 'images',
        ],
    ];
}

添加验证规则

public function rules()
{
    return [
        ['avatar', 'file'],   
    ];
}

设置正确的表单 enctype

$form = \yii\bootstrap\ActiveForm::begin([
    'enableClientValidation' => false,
    'options' => [
        'enctype' => 'multipart/form-data',
    ],
]);

文件应能够正常上传。

许可证

MIT