2025-08-07 20:21:47 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\common\controller;
|
|
|
|
|
|
|
|
|
|
|
|
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
|
|
|
|
|
|
use OSS\OssClient;
|
|
|
|
|
|
use OSS\Core\OssException;
|
2025-10-20 18:11:39 +08:00
|
|
|
|
use Qcloud\Cos\Client;
|
2025-10-23 17:11:45 +08:00
|
|
|
|
use QCloud\COSSTS\Sts;
|
2025-08-07 20:21:47 +08:00
|
|
|
|
|
|
|
|
|
|
class Upload
|
|
|
|
|
|
{
|
|
|
|
|
|
// 显式声明属性
|
|
|
|
|
|
private $config;
|
|
|
|
|
|
private $ossClient; // 修复点:添加该属性声明
|
|
|
|
|
|
private $bucket;
|
2025-10-20 18:11:39 +08:00
|
|
|
|
private $cosbucket;
|
2025-08-07 20:21:47 +08:00
|
|
|
|
public function __construct()
|
|
|
|
|
|
{
|
2025-10-20 18:11:39 +08:00
|
|
|
|
// $this->config = get_system_config();
|
|
|
|
|
|
$endpoint = get_system_config_value('oss_region_url');
|
|
|
|
|
|
$this->bucket= get_system_config_value('oss_bucket_name');
|
|
|
|
|
|
$ossId = get_system_config_value('oss_access_key_id');
|
|
|
|
|
|
$ossSecret = get_system_config_value('oss_access_key_secret');
|
|
|
|
|
|
|
2025-10-23 17:11:45 +08:00
|
|
|
|
$this->region = get_system_config_value('cos_region');
|
|
|
|
|
|
$this->cosId = get_system_config_value('cos_id');
|
|
|
|
|
|
$this->cosSecret = get_system_config_value('cos_secret');
|
2025-10-20 18:11:39 +08:00
|
|
|
|
$this->cosbucket= get_system_config_value('cos_bucket_name');
|
2025-10-23 17:11:45 +08:00
|
|
|
|
$this->url= get_system_config_value('cos_http_url');
|
2025-08-07 20:21:47 +08:00
|
|
|
|
//获取配置
|
|
|
|
|
|
$this->ossClient = new OssClient(
|
2025-10-20 18:11:39 +08:00
|
|
|
|
$ossId,
|
|
|
|
|
|
$ossSecret,
|
2025-08-07 20:21:47 +08:00
|
|
|
|
$endpoint
|
|
|
|
|
|
);
|
2025-10-20 18:11:39 +08:00
|
|
|
|
|
|
|
|
|
|
// 初始化COS客户端
|
|
|
|
|
|
$this->cosClient = new Client([
|
2025-10-23 17:11:45 +08:00
|
|
|
|
'region' => $this->region, // 你的存储桶所属地域,如 `ap-guangzhou`
|
2025-10-20 18:11:39 +08:00
|
|
|
|
'credentials' => [
|
2025-10-23 17:11:45 +08:00
|
|
|
|
'secretId' => $this->cosId,
|
|
|
|
|
|
'secretKey' => $this->cosSecret,
|
2025-10-20 18:11:39 +08:00
|
|
|
|
],
|
|
|
|
|
|
]);
|
2025-08-07 20:21:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2025-10-20 18:11:39 +08:00
|
|
|
|
* 上传文件 -阿里oss
|
2025-08-07 20:21:47 +08:00
|
|
|
|
* 参数:
|
|
|
|
|
|
* $bucket: 存储空间名称
|
|
|
|
|
|
* $object: 文件名
|
|
|
|
|
|
* $filePath: 文件路径
|
|
|
|
|
|
* 返回:
|
|
|
|
|
|
* true: 上传成功
|
|
|
|
|
|
* false: 上传失败
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function uploadFile($object, $filePath) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
$result = $this->ossClient->uploadFile($this->bucket, $object, $filePath);
|
|
|
|
|
|
return true; // 上传成功返回true
|
|
|
|
|
|
} catch (OssException $e) {
|
|
|
|
|
|
return false; // 上传失败返回false并记录错误信息,例如:echo $e->getMessage();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-20 18:11:39 +08:00
|
|
|
|
/*
|
|
|
|
|
|
* 上传文件 -腾讯cos
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
public function uploadFileCos($object, $filePath) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
$result = $this->cosClient->putObject([
|
|
|
|
|
|
'Bucket' => $this->cosbucket,
|
2025-10-23 17:11:45 +08:00
|
|
|
|
'Key' => 'admin/'.$object,
|
2025-10-20 18:11:39 +08:00
|
|
|
|
'Body' => fopen($filePath, 'rb'),
|
|
|
|
|
|
]);
|
|
|
|
|
|
// 上传成功,可以处理$result
|
2025-10-23 17:11:45 +08:00
|
|
|
|
// var_dump($result);exit; cos_http_url
|
|
|
|
|
|
return $this->url.'/'.$result['Key']; // 或根据你的需要拼接访问URL
|
|
|
|
|
|
|
2025-10-20 18:11:39 +08:00
|
|
|
|
} catch (OssException $e) {
|
2025-10-23 17:11:45 +08:00
|
|
|
|
return $e->getMessage(); // 上传失败返回false并记录错误信息,例如:echo $e->getMessage();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* 获取腾讯COS临时密钥
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function getTempCredential()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
$sts = new Sts();
|
|
|
|
|
|
$tempKeys = $sts->getTempKeys([
|
|
|
|
|
|
'url' => 'https://sts.tencentcloudapi.com/',
|
|
|
|
|
|
'domain' => 'sts.tencentcloudapi.com',
|
|
|
|
|
|
'proxy' => '',
|
|
|
|
|
|
'secretId' => $this->cosId,
|
|
|
|
|
|
'secretKey' => $this->cosSecret,
|
|
|
|
|
|
'bucket' => $this->cosbucket,
|
|
|
|
|
|
'region' => $this->region,
|
|
|
|
|
|
'durationSeconds' => 7200,
|
|
|
|
|
|
'allowActions' => [
|
|
|
|
|
|
// 所有 COS 操作,可根据需要限定为具体操作,如 'cos:PutObject'
|
|
|
|
|
|
'cos:*'
|
|
|
|
|
|
],
|
|
|
|
|
|
'allowPrefix' => ['images/*'], // 可进一步限制前缀
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
'code' => 1,
|
|
|
|
|
|
'msg' => 'success',
|
|
|
|
|
|
'data' => [
|
|
|
|
|
|
'credentials' => $tempKeys['credentials'],
|
|
|
|
|
|
'startTime' => $tempKeys['startTime'],
|
|
|
|
|
|
'expiredTime' => $tempKeys['expiredTime'],
|
|
|
|
|
|
'region' => $this->region,
|
|
|
|
|
|
'bucket' => $this->cosbucket,
|
|
|
|
|
|
]
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
|
return ['code' => 0, 'msg' => '临时密钥获取失败: ' . $e->getMessage()];
|
2025-10-20 18:11:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-07 20:21:47 +08:00
|
|
|
|
|
|
|
|
|
|
}
|