basePath."/config/huaweicloud_".YII_ENV."_conf.php"); global $BUCKET; global $ENDPOINT; global $ACCESSKEY; global $SECRETKEY; global $TIMEOUT; $this->bucket=$BUCKET; $this->accessKey=$ACCESSKEY; $this->secretKey=$SECRETKEY; $this->endpoint=$ENDPOINT; $this->timeOut=$TIMEOUT; $this->cosClient = new ObsClient ( [ 'key' => $this->accessKey, 'secret' => $this->secretKey, 'endpoint' => $this->endpoint ] ); } public function putFile($key,$localFile,$storageClass=''){ $data=array( 'status'=>0 ); if(!file_exists($localFile)){ $data=array( 'msg'=>"文件不存在" ); return $data; } try { $option=array(); if($storageClass){ $option['StorageClass']=$storageClass; } $result = $this->cosClient->putObject([ 'Bucket' => $this->bucket, 'Key' => $key, 'SourceFile' => $localFile ]); if($result && isset($result['HttpStatusCode']) && $result['HttpStatusCode']=='200'){ $data['status']=1; $data['url']=$result['ObjectURL']; } } catch (\Exception $e) { // 请求失败 //echo($e); $data['msg']=$e; } return $data; } //删除 key 就是url去掉前面的域名部分 public function deleteFile($key){ $object=array( 'Bucket'=>$this->bucket, 'Key'=>$key ); $result = array( 'status' => 0, 'msg' => '', 'data' => '', ); try { $result = $this->cosClient->deleteObject($object); // 请求成功 $result['status'] = 1; $result['data'] = ''; $result['msg'] = "delete $this->bucket/$key success"; } catch (\Exception $e) { // 请求失败 $result['msg'] = '删除失败'; } return $result; } //修改文档属性 public function updateFile($bucket,$key,$source){ $result['status'] = 0; try { $rs = $this->cosClient->Copy($bucket,$key,$source,array('StorageClass'=>'STANDARD_IA')); if($rs=='1001'){ $result['status'] = -1; $result['msg'] = '文件已是低频存储'; }else{ // 请求成功 $result['status'] = 1; $result['data'] = ''; $result['msg'] = "copy $this->bucket/$key success"; } } catch (\Exception $e) { // 请求失败 $result['msg'] = '更新失败'; } return $result; } }