文件上传
This commit is contained in:
@@ -53,12 +53,19 @@ class UploadFile extends Upload
|
|||||||
$objectName = $fileName . '_' . $uniqueId . '.' . $extension;
|
$objectName = $fileName . '_' . $uniqueId . '.' . $extension;
|
||||||
|
|
||||||
// 调用父类方法上传到 OSS
|
// 调用父类方法上传到 OSS
|
||||||
$result = $this->uploadFile($objectName, $filePath);
|
// $result = $this->uploadFile($objectName, $filePath);
|
||||||
|
// if (!$result) {
|
||||||
|
// return V(0, '上传失败,请检查OSS配置或网络', null);
|
||||||
|
// }
|
||||||
|
// // 返回访问地址
|
||||||
|
// $url = $this->config['oss_cdn_url'] . $objectName;
|
||||||
|
|
||||||
|
// 调用父类方法上传到 COS
|
||||||
|
$result = $this->uploadFileCos($objectName, $filePath);
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
return V(0, '上传失败,请检查OSS配置或网络', null);
|
return V(0, '上传失败,请检查COS配置或网络', null);
|
||||||
}
|
}
|
||||||
// 返回访问地址
|
$url = $result;// 返回访问地址 具体访问地址根据你的实际配置来定这个需要修改 等域名通过备案后修改
|
||||||
$url = $this->config['oss_cdn_url'] . $objectName;
|
|
||||||
return V(1, '上传成功', ['url' => $url]);
|
return V(1, '上传成功', ['url' => $url]);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return V(0, '上传异常: ' . $e->getMessage(), null);
|
return V(0, '上传异常: ' . $e->getMessage(), null);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace app\common\controller;
|
|||||||
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
|
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
|
||||||
use OSS\OssClient;
|
use OSS\OssClient;
|
||||||
use OSS\Core\OssException;
|
use OSS\Core\OssException;
|
||||||
|
use Qcloud\Cos\Client;
|
||||||
|
|
||||||
class Upload
|
class Upload
|
||||||
{
|
{
|
||||||
@@ -12,21 +13,38 @@ class Upload
|
|||||||
private $config;
|
private $config;
|
||||||
private $ossClient; // 修复点:添加该属性声明
|
private $ossClient; // 修复点:添加该属性声明
|
||||||
private $bucket;
|
private $bucket;
|
||||||
|
private $cosbucket;
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->config = get_system_config();
|
// $this->config = get_system_config();
|
||||||
$endpoint = $this->config['oss_region_url'];
|
$endpoint = get_system_config_value('oss_region_url');
|
||||||
$this->bucket= $this->config['oss_bucket_name'];
|
$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');
|
||||||
|
|
||||||
|
$region = get_system_config_value('cos_region');
|
||||||
|
$cosId = get_system_config_value('cos_id');
|
||||||
|
$cosSecret = get_system_config_value('cos_secret');
|
||||||
|
$this->cosbucket= get_system_config_value('cos_bucket_name');
|
||||||
//获取配置
|
//获取配置
|
||||||
$this->ossClient = new OssClient(
|
$this->ossClient = new OssClient(
|
||||||
$this->config['oss_access_key_id'],
|
$ossId,
|
||||||
$this->config['oss_access_key_secret'],
|
$ossSecret,
|
||||||
$endpoint
|
$endpoint
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 初始化COS客户端
|
||||||
|
$this->cosClient = new Client([
|
||||||
|
'region' => $region, // 你的存储桶所属地域,如 `ap-guangzhou`
|
||||||
|
'credentials' => [
|
||||||
|
'secretId' => $cosId,
|
||||||
|
'secretKey' => $cosSecret,
|
||||||
|
],
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 上传文件
|
* 上传文件 -阿里oss
|
||||||
* 参数:
|
* 参数:
|
||||||
* $bucket: 存储空间名称
|
* $bucket: 存储空间名称
|
||||||
* $object: 文件名
|
* $object: 文件名
|
||||||
@@ -44,5 +62,23 @@ class Upload
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 上传文件 -腾讯cos
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function uploadFileCos($object, $filePath) {
|
||||||
|
try {
|
||||||
|
$result = $this->cosClient->putObject([
|
||||||
|
'Bucket' => $this->cosbucket,
|
||||||
|
'Key' => $object,
|
||||||
|
'Body' => fopen($filePath, 'rb'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 上传成功,可以处理$result
|
||||||
|
return $result['Key']; // 或根据你的需要拼接访问URL
|
||||||
|
} catch (OssException $e) {
|
||||||
|
return false; // 上传失败返回false并记录错误信息,例如:echo $e->getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user