|
|
|
|
<?php
|
|
|
|
|
namespace Base\Tool;
|
|
|
|
|
|
|
|
|
|
use Base\Tool\StorageClient;
|
|
|
|
|
use Exception;
|
|
|
|
|
use Qcloud\Cos\Client as BaseCosClient;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 腾讯云OBS
|
|
|
|
|
*/
|
|
|
|
|
class CosClient implements StorageClient
|
|
|
|
|
{
|
|
|
|
|
private $secretId;
|
|
|
|
|
private $secretKey;
|
|
|
|
|
private $region;
|
|
|
|
|
private $domain;
|
|
|
|
|
private $bucket;
|
|
|
|
|
private $client;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->domain = C('COS_DOMAIN');
|
|
|
|
|
$this->region = C('COS_REGION');
|
|
|
|
|
$this->bucket = C('COS_BUCKET');
|
|
|
|
|
$this->secretId = C('COS_SECRET_ID');
|
|
|
|
|
$this->secretKey = C('COS_SECRET_KEY');
|
|
|
|
|
$this->client = new BaseCosClient(
|
|
|
|
|
[
|
|
|
|
|
'region' => $this->region,
|
|
|
|
|
'scheme' => 'https', //协议头部,默认为http
|
|
|
|
|
'credentials'=> [
|
|
|
|
|
'secretId' => $this->secretId,
|
|
|
|
|
'secretKey' => $this->secretKey,
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function upload($localFilePath, $saveFileName)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$file = fopen($localFilePath, 'rb');
|
|
|
|
|
if (!$file) {
|
|
|
|
|
throw new Exception('读取文件失败');
|
|
|
|
|
}
|
|
|
|
|
$result = $this->client->Upload(
|
|
|
|
|
$this->bucket,
|
|
|
|
|
$saveFileName,
|
|
|
|
|
$file
|
|
|
|
|
);
|
|
|
|
|
return [
|
|
|
|
|
'status' => true,
|
|
|
|
|
'message' => '上传成功',
|
|
|
|
|
'data' => [
|
|
|
|
|
'url' => $this->getUrl($saveFileName)
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return [
|
|
|
|
|
'status' => false,
|
|
|
|
|
'message' => $e->getMessage()
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete($deleteFile)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$result = $this->client->deleteObject(array(
|
|
|
|
|
'Bucket' => $this->bucket,
|
|
|
|
|
'Key' => $deleteFile
|
|
|
|
|
));
|
|
|
|
|
return [
|
|
|
|
|
'status' => true,
|
|
|
|
|
'message' => '删除成功',
|
|
|
|
|
];
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return [
|
|
|
|
|
'status' => false,
|
|
|
|
|
'message' => $e->getMessage()
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getUrl($saveFileName)
|
|
|
|
|
{
|
|
|
|
|
return $this->domain . '/' . $saveFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|