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' => ['images/*'], // 可进一步限制前缀 ]); 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()]; } } }