Files
2025-11-13 15:24:15 +08:00

129 lines
4.1 KiB
PHP
Raw Permalink 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 app\common\controller;
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
use Qcloud\Cos\Client;
use QCloud\COSSTS\Sts;
class Upload
{
// 显式声明属性
private $config;
private $ossClient; // 修复点:添加该属性声明
private $bucket;
private $cosbucket;
public function __construct()
{
// $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');
$this->region = get_system_config_value('cos_region');
$this->cosId = get_system_config_value('cos_id');
$this->cosSecret = get_system_config_value('cos_secret');
$this->cosbucket= get_system_config_value('cos_bucket_name');
$this->url= get_system_config_value('cos_http_url');
//获取配置
$this->ossClient = new OssClient(
$ossId,
$ossSecret,
$endpoint
);
// 初始化COS客户端
$this->cosClient = new Client([
'region' => $this->region, // 你的存储桶所属地域,如 `ap-guangzhou`
'credentials' => [
'secretId' => $this->cosId,
'secretKey' => $this->cosSecret,
],
]);
}
/*
* 上传文件 -阿里oss
* 参数:
* $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();
}
}
/*
* 上传文件 -腾讯cos
*/
public function uploadFileCos($object, $filePath) {
try {
$result = $this->cosClient->putObject([
'Bucket' => $this->cosbucket,
'Key' => 'admin/'.$object,
'Body' => fopen($filePath, 'rb'),
]);
// 上传成功,可以处理$result
// var_dump($result);exit; cos_http_url
return $this->url.'/'.$result['Key']; // 或根据你的需要拼接访问URL
} catch (OssException $e) {
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' => ['*'], // 可进一步限制前缀
]);
return [
'code' => 1,
'msg' => 'success',
'data' => [
'credentials' => $tempKeys['credentials'],
'startTime' => $tempKeys['startTime'],
'expiredTime' => $tempKeys['expiredTime'],
'region' => $this->region,
'bucket' => $this->cosbucket,
'url' => $this->url,
]
];
} catch (\Exception $e) {
return ['code' => 0, 'msg' => '临时密钥获取失败: ' . $e->getMessage()];
}
}
}