The Qiniu integration for the Yii framework
The preferred way to install this extension is through composer.
Either run
composer require --prefer-dist dcb9/yii2-qiniu
or add
"dcb9/yii2-qiniu": "*"
to the require
section of your composer.json.
To use this extension, simply add the following code in your application configuration:
return [
//....
'components' => [
'qiniu' => [
'class' => 'dcb9\qiniu\Component',
'accessKey' => 'YOUR_ACCESS_KEY',
'secretKey' => 'YOUR_SECRET_KEY',
'disks' => [
'testBucket' => [
'bucket' => 'bucketOnQiniu',
'baseUrl' => 'ACCESS_QINIU_URL',
'isPrivate' => true,
'zone' => 'zone0', // 可设置为 zone0, zone1 @see \Qiniu\Zone
],
],
],
],
];
资源操作就 Flysystem 的一个扩展, 所以所有的调用方法与 Flysystem 调用方法一致.
// 获取 Disk
$filesystem = Yii::$app->qiniu->getDisk('testBucket');
$filesystem->has('hello.txt');
// 七牛独有 API
$filesystem->getUrl('hello.txt'); // 获取访问地址
默认设置 Policy 是使用 Array 的方式来设置的, 但是这种方式对程序员不是很友好,于是创建了一个 Policy 的类,但所有的操作还是跟操作数组一样.
$policy = new \dcb9\qiniu\Policy();
$policy->callbackUrl = '';
$policy->callbackBody = '';
$qiniu = Yii::$app->qiniu;
$diskName = 'testBucket';
$token1 = $qiniu->getUploadToken($diskName);
$key = null;
$expires = 3600;
$policy = new \dcb9\qiniu\Policy();
$policy->callbackUrl = '';
$policy->callbackBody = '';
// Fop @see src/Pfop.php
$policy->persistentOps = \dcb9\qiniu\Pfop::instance()
->avthumb('mp4')
->wmImage('http://o82pobmde.bkt.clouddn.com/yii2-logo.png')
->saveas('testbucket', 'after-ops' . date('Y-m-d H:i:s') . '.mp4')
->__toString();
$policy->persistentNotifyUrl = 'http://blog.phpor.me';
$diskName = 'testBucket';
$token2 = $qiniu->getUploadToken($diskName, $key, $expires, $policy);
$token = '<TOKEN>'; // @see 获取 UploadToken
$config = ['token' => $token];
$filesystem->writeStream($path, $stream, $config);
$filesystem->write($path, $content, $config);
$filesystem->put($path, $content, $config);
- 给配置的组件加 IDE 自动补全 IDE autocompletion for custom components