aliossChajian.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * 阿里云oss服务
  4. */
  5. class aliossChajian extends Chajian{
  6. private $accesskeyid;
  7. private $accesskeysecret;
  8. private $vendorbool=false;
  9. private $ossClient=false;
  10. protected function initChajian()
  11. {
  12. $this->accesskeyid = getconfig('alioss_keyid');
  13. $this->accesskeysecret = getconfig('alioss_keysecret');
  14. $this->folder = getconfig('alioss_folder');
  15. $this->bucket = getconfig('alioss_bucket');
  16. $path = ''.ROOT_PATH.'/include/vendor/autoload.php';
  17. if(file_exists($path) && $this->accesskeysecret){
  18. require_once($path);
  19. $this->vendorbool = true;
  20. }
  21. }
  22. public function isbool()
  23. {
  24. return $this->vendorbool;
  25. }
  26. private function getOssClient()
  27. {
  28. if(!$this->ossClient){
  29. $fq = getconfig('alioss_region');
  30. $endpoint = 'http://oss-cn-'.$fq.'.aliyuncs.com';
  31. $this->ossClient = new \OSS\OssClient($this->accesskeyid, $this->accesskeysecret, $endpoint);
  32. }
  33. return $this->ossClient;
  34. }
  35. /**
  36. * 上传文件到oss
  37. */
  38. public function uploadFile($path)
  39. {
  40. if(!$this->isbool())return returnerror('no install alioss');
  41. try{
  42. $ossClient = $this->getOssClient();
  43. $barr = $ossClient->uploadFile($this->bucket, $this->folder.'/'.$path, ROOT_PATH.'/'.$path);
  44. if(is_array($barr)){
  45. $info = $barr['info'];
  46. $carr = returnsuccess($barr);
  47. $carr['code']= 0;
  48. $carr['url'] = str_replace('http:','https:', $info['url']);
  49. $carr['filesize'] = $info['request_size'];
  50. return $carr;
  51. }else{
  52. return returnerror();
  53. }
  54. } catch(\OSS\Core\OssException $e) {
  55. return returnerror($e->getMessage());
  56. }
  57. }
  58. /**
  59. * 下载文件
  60. */
  61. public function download($path, $dstPath)
  62. {
  63. if(!$this->isbool())return returnerror('no install alioss');
  64. try{
  65. $ossClient = $this->getOssClient();
  66. $localfile = $dstPath;
  67. $options = array(
  68. \OSS\OssClient::OSS_FILE_DOWNLOAD => $localfile
  69. );
  70. $ossClient->getObject($this->bucket, $path, $options);
  71. $carr = returnsuccess($barr);
  72. $carr['code']= 0;
  73. return $carr;
  74. } catch(\OSS\Core\OssException $e) {
  75. return returnerror($e->getMessage());
  76. }
  77. }
  78. }
粤ICP备19079148号