zoukankan      html  css  js  c++  java
  • 记腾讯云 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;
        }
    
    }
  • 相关阅读:
    [转]Ant入门教程
    [转]如何用CruiseControl.Net来进行持续化集成
    [书目20130216]深入浅出WPF
    [转]WF事件驱动(4) 持久化
    [转]WF4.0 基础篇 (一)开始使用WF
    [转]VS2010&.Net 4.0 之并行运算(Parallel)(For、Foreach)
    [转]SVN + CruiseControl.NET + NANT 自动编译提交的项目最小DEMO
    [转]WF4.0 基础篇 (六) 数据的传递 Arguments 参数
    [转]WF事件驱动(1)
    [转]WCF开发简简单单的六个步骤
  • 原文地址:https://www.cnblogs.com/liiu/p/14885572.html
Copyright © 2011-2022 走看看