dcb9/yii2-qiniu

This package is abandoned and no longer maintained. No replacement package was suggested.

The Qiniu integration for the Yii framework

Installs: 1 606

Dependents: 1

Suggesters: 0

Security: 0

Stars: 17

Watchers: 2

Forks: 9

Open Issues: 1

Type:yii2-extension

v0.5 2016-10-14 09:53 UTC

This package is not auto-updated.

Last update: 2020-01-24 15:52:24 UTC


README

The Qiniu integration for the Yii framework

Build Status Code Climate Issue Count Latest Stable Version Total Downloads License

CHANGE LOG

Installation

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.

Configuration

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');  // 获取访问地址

其它所有可调用的 API

上传策略

默认设置 Policy 是使用 Array 的方式来设置的, 但是这种方式对程序员不是很友好,于是创建了一个 Policy 的类,但所有的操作还是跟操作数组一样.

$policy = new \dcb9\qiniu\Policy();
$policy->callbackUrl = '';
$policy->callbackBody = '';

获取 UploadToken

$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 = '<TOKEN>'; // @see 获取 UploadToken
$config = ['token' => $token];
$filesystem->writeStream($path, $stream, $config);

$filesystem->write($path, $content, $config);

$filesystem->put($path, $content, $config);

Tricks