ossUrl = getOssUrl(); $this->ossInerUrl = getOssInterUrl(); $this->accessKeyId = Yii::$app->params['oss']['OSS_ACCESS_KEY']; $this->accessKeySecret = Yii::$app->params['oss']['OSS_ACCESS_SECRET']; $this->endpoint = $internal?Yii::$app->params['oss']['OSS_INTERNAL_ENDPOINT']:Yii::$app->params['oss']['OSS_ENDPOINT']; $this->bucket = Yii::$app->params['oss']['OSS_BUCKET']; $this->region = Yii::$app->params['oss']['OSS_REGION']; $this->type = Yii::$app->params['oss']['OSS_TYPE']; $this->cname = FALSE; $result = []; if($this->type=='ali') { try { $this->ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint,$this->cname); $result = array('error'=>0); } catch (OssException $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } else if($this->type=='tencent') { $protocol = str_replace("://","",SITE_PROTOCOL); $this->ossClient = new \Qcloud\Cos\Client( array( 'region' => $this->region, 'schema' => $protocol, //协议头部,默认为http 'credentials'=> array( 'secretId' => $this->accessKeyId , 'secretKey' => $this->accessKeySecret))); $result = array('error'=>0); } else if($this->type=='baidu') { $this->ossClient = new BosClient(array( 'credentials' => array( 'accessKeyId' => $this->accessKeyId, 'secretAccessKey' => $this->accessKeySecret, ), 'endpoint' => $this->endpoint )); $result = array('error'=>0); } else if($this->type=='s3') { $this->ossClient = new S3Client([ 'version' => 'latest', 'region' => $this->region, 'endpoint' => 'https://' . str_replace(array('https://','http://'),'',$this->endpoint), 'credentials' => [ 'key' => $this->accessKeyId, 'secret' => $this->accessKeySecret, ] ]); } return $result; } /* * $file:完整文件路径 * $object:文件存放到OSS的相对路径 */ public function Upload($file,$object) { $result = []; $data = []; if($this->type=='ali') { try{ $ossresult = $this->ossClient->uploadFile($this->bucket, $object, $file); $ossresult['info']['url'] = str_replace(Yii::$app->params['oss']['OSS_INTERNAL_ENDPOINT'],Yii::$app->params['oss']['OSS_ENDPOINT'],$ossresult['info']['url']); $data['url'] = str_replace(array('https://','http://'),SITE_PROTOCOL,$ossresult['info']['url']); $data['path'] = $object; $data['size'] = $ossresult['info']['upload_content_length']; $result = array('error'=>0,'data'=>$data); } catch(OssException $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } else if($this->type=='tencent') { try { $fileContent = fopen($file, "rb"); if ($fileContent) { $ossresult = $this->ossClient->putObject(array( 'Bucket' => $this->bucket, 'Key' => $object, 'Body' => $fileContent)); $data['url'] = SITE_PROTOCOL.$ossresult['Location']; $data['path'] = $object; $data['size'] = filesize($file); $result = array('error'=>0,'data'=>$data); } } catch (\Exception $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } else if($this->type=='baidu') { try { $this->ossClient->putObjectFromFile($this->bucket,$object,$file); $data['url'] = SITE_PROTOCOL.$this->bucket.'.'.$this->endpoint.'/'.$object; $data['path'] = $object; $data['size'] = filesize($file); $result = array('error'=>0,'data'=>$data); } catch (\Exception $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } else if($this->type=='s3') { try { $key = $object; $source = fopen($file, 'rb'); $acl = 'public-read'; $uploader = new ObjectUploader( $this->ossClient, $this->bucket, $key, $source, $acl ); $ossresult = $uploader->upload(); if ($ossresult["@metadata"]["statusCode"] == '200') { $data['url'] = $ossresult["ObjectURL"]; $data['path'] = $object; $data['size'] = filesize($file); $result = array('error'=>0,'data'=>$data); } } catch (\Exception $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } return $result; } /** * @param $localfile * @param $object */ public function downLoad($localfile,$object) { $object = str_replace($this->ossUrl,'',$object); $object= str_replace($this->ossInerUrl,'',$object); $result = []; if($this->type=='ali') { try{ $options = array( OssClient::OSS_FILE_DOWNLOAD => $localfile ); $this->ossClient->getObject($this->bucket, $object, $options); $result = array('error'=>0); } catch(OssException $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } else if($this->type=='tencent') { try { $key = $object; //此处的 key 为对象键,对象键是对象在存储桶中的唯一标识 $this->ossClient->getObject(array('Bucket' => $this->bucket,'Key' => $key,'SaveAs' => $localfile)); $result = array('error'=>0); } catch (\Exception $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } else if($this->type=='baidu') { try { $this->ossClient->getObjectToFile($this->bucket, $object, $localfile); $result = array('error'=>0); } catch (\Exception $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } else if($this->type=='s3') { try { $result = $this->ossClient->getObject([ 'Bucket' => $this->bucket, 'Key' => $object ]); //header("Content-Type: {$result['ContentType']}"); file_put_contents($localfile,$result['Body']); $result = array('error'=>0); } catch (S3Exception $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } return $result; } //删除单个文件 public function delete($object){ $object = str_replace($this->ossUrl,'',$object); $object= str_replace($this->ossInerUrl,'',$object); if($this->type=='ali') { try{ $ossresult = $this->ossClient->deleteObject($this->bucket, $object); $result = array('error'=>0,'data'=>'删除成功'); } catch(OssException $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } else if($this->type=='tencent') { try { $ossresult = $this->ossClient->deleteObject(array( 'Bucket' => $this->bucket, //存储桶名称 'Key' => $object )); $result = array('error'=>0,'data'=>'删除成功'); } catch (\Exception $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } else if($this->type=='baidu') { try { $ossresult = $this->ossClient->deleteObject($this->bucket, $object); $result = array('error'=>0,'data'=>'删除成功'); } catch (\Exception $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } else if($this->type=='s3') { try { $ossresult = $this->ossClient->deleteObject([ 'Bucket' => $this->bucket, 'Key' => $object ]); $result = array('error'=>0,'data'=>'删除成功'); } catch (S3Exception $e) { $result = array('error'=>1,'msg'=>$e->getMessage()); } } return $result; } } ?>