Giter Club home page Giter Club logo

yii2-qiniu's Introduction

Qiniu Extension for Yii2

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 = '';
$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

yii2-qiniu's People

Contributors

dcb9 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.