args = $args; $this->ossUrl = SITE_PROTOCOL.$args['OSS_BUCKET'].'.'.$args['OSS_ENDPOINT'].'/'; $this->ossInerUrl = SITE_PROTOCOL.$args['OSS_BUCKET'].'.'.$args['OSS_INTERNAL_ENDPOINT'].'/'; $this->accessKeyId = $args['OSS_ACCESS_KEY']; $this->accessKeySecret = $args['OSS_ACCESS_SECRET']; $this->endpoint = $args['OPEN_INTERNAL']?$args['OSS_INTERNAL_ENDPOINT']:$args['OSS_ENDPOINT'];; $this->bucket = $args['OSS_BUCKET']; $this->region = $args['OSS_REGION']; $this->type = $args['OSS_TYPE']?$args['OSS_TYPE']:$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($this->args['OSS_INTERNAL_ENDPOINT'],$this->args['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; } //删除单个文件 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; } //转换图片 public function previewImg($remoteFileUrl,$i) { if($this->type=='tencent') { return $remoteFileUrl.'?ci-process=doc-preview&page='.$i; } } } ?>