2023-11-21 15:14:59 +08:00

83 lines
1.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace hema\storage\engine;
use Qcloud\Cos\Client;
/**
* 腾讯云存储引擎 (COS)
* Class Qcloud
*/
class Qcloud extends Basics
{
// Qcloud类
private $cosClient;
/**
* 构造方法
* Qcloud constructor.
* @param string $storage 存储方式
* @param array|null $config 存储配置
*/
public function __construct(string $storage, array $config = null)
{
parent::__construct($storage, $config);
// 创建Qcloud类
$this->createCosClient();
}
/**
* 创建COS控制类
*/
private function createCosClient()
{
$this->cosClient = new Client([
'region' => $this->config['region'],
'credentials' => [
'secretId' => $this->config['secret_id'],
'secretKey' => $this->config['secret_key'],
],
]);
}
/**
* 执行上传
* @return bool|mixed
*/
public function upload()
{
// 上传文件
// putObject(上传接口最大支持上传5G文件)
try {
$result = $this->cosClient->putObject([
'Bucket' => $this->config['bucket'],
'Key' => $this->getSaveFileInfo()['file_path'],
'Body' => fopen($this->getRealPath(), 'rb')
]);
return true;
} catch (\Exception $e) {
$this->error = $e->getMessage();
return false;
}
}
/**
* 删除文件
* @param string $fileName
* @return bool|mixed
*/
public function delete(string $fileName)
{
try {
$result = $this->cosClient->deleteObject(array(
'Bucket' => $this->config['bucket'],
'Key' => $fileName
));
return true;
} catch (\Exception $e) {
$this->error = $e->getMessage();
return false;
}
}
}