七牛云,php

composer require qiniu/php-sdk

工具类

<?php
/**
 * 七牛云,工具类
 */

namespace Common\Util;
use Qiniu\Config;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
use Qiniu\Auth;

class QiniuUtil extends CommonUtil {
    protected $accessKey = '';
    protected $secretKey = '';
    public function __construct()
    {
        parent::__construct();
        $this->accessKey = C('QINIU.ACCESS_KEY');
        $this->secretKey = C('QINIU.SECRET_KEY');
    }

    /**
     * @param $file
     * @param $key
     * @param $bucket
     * @return false|mixed
     */
    public function up($file, $key, $bucket = '') {
        if (!$bucket) {
            $bucket =  C('QINIU.BUCKET');
        }
        $uploadMgr = new UploadManager();
        $auth = new Auth($this->accessKey, $this->secretKey);
        $token = $auth->uploadToken($bucket);
        list($ret, $error) = $uploadMgr->put($token, $key, file_get_contents($file));
        if (!$error) {
            return $ret;
        } else {
            return false;
        }
    }

    /**
     * 删除资源
     * @param $key
     * @param $bucket
     * @return bool
     */
    public function delKey($key,$bucket = '') {
        if (!$bucket) {
            $bucket =  C('QINIU.BUCKET');
        }
        $auth = new Auth($this->accessKey, $this->secretKey);
        $config = new Config();
        $bucketManager = new BucketManager($auth, $config);
        $err = $bucketManager->delete($bucket, $key);
        if (!$err) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 统计资源信息
     * @param $key
     * @param $bucket
     * @return bool
     */
    public function statKey($key,$bucket = '') {
        if (!$bucket) {
            $bucket =  C('QINIU.BUCKET');
        }
        $auth = new Auth($this->accessKey, $this->secretKey);
        $config = new Config();
        $bucketManager = new BucketManager($auth, $config);
        list($ret, $err) = $bucketManager->stat($bucket, $key);
        if ($err != null) {
            return false;
        } else {
            return $ret['fsize'];
        }
    }
}
posted @ 2021-07-22 12:00  TBHacker  阅读(80)  评论(0编辑  收藏  举报