<?php
|
|
namespace common;
|
|
use think\facade\Config;
|
require __DIR__ . '/cos-sdk-v5-7.phar';
|
|
class Uploadcos {
|
private $cosClient;
|
private $SecretId;
|
private $SecretKey;
|
private $appId;
|
private $region;
|
|
|
public function __construct() {
|
$this->SecretId = Config::get('qcloud.Qcloud.SecretId');
|
$this->SecretKey = Config::get('qcloud.Qcloud.SecretKey');
|
$this->appId = Config::get('qcloud.Qcloud.appId');
|
$this->region = Config::get('qcloud.Qcloud.region');
|
|
$this->cosClient = new \Qcloud\Cos\Client(
|
array(
|
'region' => $this->region,
|
'scheme' => 'https', //协议头部,默认为 http
|
'credentials' => array(
|
'secretId' => $this->SecretId,
|
'secretKey' => $this->SecretKey)
|
));
|
}
|
/*
|
* 上传文件
|
*/
|
public function cosUpload($key, $localPath) {
|
try {
|
$bucket = Config::get('qcloud.Qcloud.bucket'); //存储桶名称 格式:BucketName-APPID
|
$result = $this->cosClient->putObject(
|
array(
|
'Bucket' => $bucket,
|
'Key' => $key,
|
'Body' => fopen($localPath, 'rb')
|
));
|
return $result;
|
|
} catch (\Exception $e) {
|
return $e->getMessage();
|
}
|
}
|
/*
|
* 删除文件
|
*/
|
public function cosDelete($key, $versionId = '') {
|
try {
|
$bucket = Config::get('qcloud.Qcloud.bucket'); //存储桶名称 格式:BucketName-APPID
|
$result = $this->cosClient->deleteObject(
|
array(
|
'Bucket' => $bucket,
|
'Key' => $key,
|
'VersionId' => $versionId
|
));
|
return $result;
|
} catch (\Exception $e) {
|
return $e->getMessage();
|
}
|
}
|
|
|
}
|