<?php
require(__DIR__.'/../vendor/qcloud/cos-sdk-v5/cos-autoloader.php');
use Qcloud\Cos\Api;
use Guzzle\Service\Resource\Model;
/**
* 腾讯云上传类
*/
class TenUpload extends Model
{
private $cos_client;
private $CI;
function __construct()
{
$CI = &get_instance();
$this->CI=$CI;
$CI->load->config('services');
$this->cos_client = new Qcloud\Cos\Client($CI->config->item('region'));
}
/**
* 创建空间目录
* @Author
* @DateTime 2018-04-04
* @version 1.0
* @param [type] $buket [description]
* @return [type] [description]
*/
public function createBuket($buket){
try {
$result = $this->cos_client->createBucket(array('Bucket' => $buket));
return true;
} catch (\Exception $e) {
// echo "$e\n";
return false;
}
}
/**
* 删除空间目录
* @DateTime 2018-04-04
* @version 1.0
* @param [type] $buket [description]
* @return [type] [description]
*/
public function deleteBuket($buket){
try {
$result = $this->cos_client->deleteBucket(array(
'Bucket' => $buket));
return true;
} catch (\Exception $e) {
// echo "$e\n";
return false;
}
}
/**
* 上传文件类
* @Author
* @DateTime 2018-04-04
* @version 1.0
* @param [type] $buket [description]
* @param [type] $key [description]
* @param [type] $body [description]
* @return [type] [description]
*/
public function uploadFile($buket,$key,$body){
set_time_limit(0);
if(filesize($body) < 5242880){
//判断文件是否小于5m
try {
$result=$this->cos_client->putObject(array(
'Bucket' => $buket,
'Key' => $key,
'Body' => fopen($body,'rb'),
// 'ACL' =>'public-read'
));
$url = parse_url($result->data['ObjectURL']);
$config = $this->CI->config->item('region');
$url = $url['scheme'].'://'.$config['cdn'].$url['path'];
return $url;
} catch (CosException $e) {
// echo "$e\n";
return false;
}
}
//分片上传
try {
$result = $this->cos_client->upload(
//bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
$bucket=$buket,
$key = $key,
$body = fopen($body,'rb')
// $options = array(
// "ACL"=>'public-read',
// 'CacheControl' => 'public-read')
);
$url = parse_url($result->data['Location']);
$config = $this->CI->config->item('region');
$url = $url['scheme'].'://'.$config['cdn'].$url['path'];
return $url;
} catch (\Exception $e) {
// return echo "$e\n";
return false;
}
}
/**
* 删除文件
* @Author
* @DateTime 2018-04-04
* @version 1.0
* @param [type] $buket [description]
* @param [type] $key [description]
* @return [type] [description]
*/
public function deleteFile($buket,$key){
// 删除COS对象
try{
$result = $this->cos_client->deleteObject(array(
//bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
'Bucket' => $buket,
'Key' => $key));
return true ;
}catch(\Exception $e) {
return false;
}
}
}
上传图片运用
/**
* cos上传
* @Author
* @DateTime 2018-04-09
*/
public function uploadImg(){
if(!isset($_FILES['file'])) $this->response(200000203,'');
$scr = $_FILES['file']['tmp_name'];
$ext = substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],'.')+1); // 上传文件后缀
$dst = md5(time()).'.'.$ext; //上传文件名称
$this->load->library('TenUpload');
$this->config->load('services');
$config = $this->config->item('region'); //加载COS配置
$buket = $config['credentials']['buket'];
$con = $this->config->item('catalog');
$url = $this->tenupload->uploadFile($buket,$con['images'].$dst,$scr);
$data = array('url' =>$url);
$this->response(0,'上传成功',$data);
}