• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

七牛云,php

开发技术 开发技术 3小时前 1次浏览
composer require qiniu/php-sdk

工具

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

namespace CommonUtil;
use QiniuConfig;
use QiniuStorageBucketManager;
use QiniuStorageUploadManager;
use QiniuAuth;

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;
        }
    }
}

程序员灯塔
转载请注明原文链接:七牛云,php
喜欢 (0)