• 记腾讯云 cos上传图片类


    自己封装:

    <?php
    namespace CosUpload;
    use QcloudCosClient;
    
    
    class CosUpload
    {
        //上传图片压缩包
        public static function sendzipimg($imgurl,$codeName,$isdel=0){
            return self::sendimg($imgurl,$codeName,'zipimg',$isdel);
        }
        
        /**
         * 腾讯对象存储-文件上传
         * @datatime 2018/05/17 09:20
         * @author lgp
         */
        public function goodsCosUp($tmpImg,$fileName,$path='goods_pic/'){
            
            $date = date('Y-m-d');
            $path = $path.$date.'/';
            $cosClient = new Client(array('region' =>config('app.cos.region'),'credentials'=> array('secretId' =>config('app.cos.secretId'),'secretKey' =>config('app.cos.secretKey'))));
            
            try {
                $result = $cosClient->putObject(array(
                    'Bucket' => config('app.cos.bucket'),
                    'Key' => $path.$fileName,
                    'Body' => fopen($tmpImg, 'rb')));
            } catch (Exception $e) {
               return false;
            }
    
            return     $result['Location'];
    
        }     
        
        public function otherCosUp($tmpImg,$fileName,$path='banner_pic/'){
    
            $path = $path.'/';
            $cosClient = new Client(array('region' =>config('app.cos.region'),'credentials'=> array('secretId' =>config('app.cos.secretId'),'secretKey' =>config('app.cos.secretKey'))));
            
            try {
                $result = $cosClient->putObject(array(
                    'Bucket' => config('app.cos.bucket'),
                    'Key' => $path.$fileName,
                    'Body' => fopen($tmpImg, 'rb')));
            } catch (Exception $e) {
               return false;
            }
    
            return     $result['Location'];
    
        }      
        
        
        public static  function delimg($url){
            
            $cosClient = new Client(array('region' =>config('app.cos.region'),'credentials'=> array('secretId' =>config('app.cos.secretId'),'secretKey' =>config('app.cos.secretKey'))));
            $Key=str_replace(config('app.cos.host'),'',$url);
    
            try {
                $result = $cosClient->deleteObject(array(
                   'Bucket' =>  config('app.cos.bucket'),
                   'Key' => $Key,
                //   'VersionId' => 'string'
                ));            
            } catch (Exception $e) {
    
               return false;
            }
    
            return true;
        }    
        
        //下载文件到本地
        public static function downLoad($url,$downPath){
            
            $cosClient = new Client(array('region' =>config('app.cos.region'),'credentials'=> array('secretId' =>config('app.cos.secretId'),'secretKey' =>config('app.cos.secretKey'))));
            
            $Key=str_replace(config('app.cos.host'),'',$url);      
            try {
              $result = $cosClient->getObject(array(
                  'Bucket' => config('app.cos.bucket'),
                  'Key' => $Key,
                  'SaveAs' => $downPath
              )); 
              // 请求成功
            } catch (Exception $e) {
              
               return false;
            }   
            
              return true;
        }
    
    }
  • 相关阅读:
    C#之泛型
    etcd 使用: golang 例子
    九卷读书:《高效能人士的7个习惯》脑图
    Go package(2) strings 用法
    技术管理:技术管理者的多维度能力及成长路径
    gin框架教程:代码系列demo地址
    五大常见的MySQL高可用方案
    gin框架教程三:JWT的使用
    九卷读书:商业模式画布
    Go package(1) time 用法
  • 原文地址:https://www.cnblogs.com/liiu/p/14885572.html
Copyright © 2020-2023  润新知