zoukankan      html  css  js  c++  java
  • 七牛云,php

    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;
            }
        }
    
        /**
         * 统计资源信息
         * @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'];
            }
        }
    }
    
  • 相关阅读:
    微信小程序-物流api
    flutter第一课
    git新手配置(ios环境)
    git新手配置(windows环境)
    .Net Core WebApi(三)——操作Oracle数据库
    .Net Core WebApi(二)——添加Nlog
    .Net Core WebApi(一)——添加Swagger
    SQL server、Oracle中拿到新增列时的自增字段值
    Sql Server中如何删除字段的自增标识
    Sql Server中删除一个字段的默认值
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/15043479.html
Copyright © 2011-2022 走看看