OssServer.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Server\Upload;
  3. use App\Model\SystemConfig;
  4. use JohnLui\AliyunOSS;
  5. use Exception;
  6. use DateTime;
  7. class OssServer
  8. {
  9. /* 城市名称:
  10. *
  11. * 经典网络下可选:杭州、上海、青岛、北京、张家口、深圳、香港、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜
  12. * VPC 网络下可选:杭州、上海、青岛、北京、张家口、深圳、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜
  13. */
  14. private $city = '北京';
  15. // 经典网络 or VPC
  16. private $networkType = '经典网络';
  17. private $AccessKeyId;
  18. private $AccessKeySecret;
  19. private $ossClient;
  20. /**
  21. * 私有初始化 API,非 API,不用关注
  22. * @param boolean 是否使用内网
  23. */
  24. public function __construct($isInternal = false)
  25. {
  26. $appKey = SystemConfig::get('ali_config', 'appKey', '');
  27. $appSecret = SystemConfig::get('ali_config', 'appSecret', '');
  28. // $appKey = SystemConfig::get('ali_config', 'appKey', '');
  29. if ($this->networkType == 'VPC' && !$isInternal) {
  30. throw new Exception("VPC 网络下不提供外网上传、下载等功能");
  31. }
  32. $this->ossClient = AliyunOSS::boot(
  33. $this->city = '北京',
  34. $this->networkType,
  35. $isInternal,
  36. $this->AccessKeyId = $appKey,
  37. $this->AccessKeySecret = $appSecret
  38. );
  39. }
  40. /**
  41. * 使用外网上传文件
  42. * @param string bucket名称
  43. * @param string 上传之后的 ossServer object 名称
  44. * @param string 上传文件路径
  45. * @return bool 上传是否成功
  46. */
  47. public static function publicUpload($bucketName, $ossKey, $filePath, $options = [])
  48. {
  49. $oss = new ossServer();
  50. $oss->ossClient->setBucket($bucketName);
  51. return $oss->ossClient->uploadFile($ossKey, $filePath, $options);
  52. }
  53. /**
  54. * 使用阿里云内网上传文件
  55. * @param string bucket名称
  56. * @param string 上传之后的 ossServer object 名称
  57. * @param string 上传文件路径
  58. * @return boolean 上传是否成功
  59. */
  60. public static function privateUpload($bucketName, $ossKey, $filePath, $options = [])
  61. {
  62. $oss = new ossServer(true);
  63. $oss->ossClient->setBucket($bucketName);
  64. return $oss->ossClient->uploadFile($ossKey, $filePath, $options);
  65. }
  66. /**
  67. * 使用外网直接上传变量内容
  68. * @param string bucket名称
  69. * @param string 上传之后的 ossServer object 名称
  70. * @param string 上传的变量
  71. * @return boolean 上传是否成功
  72. */
  73. public static function publicUploadContent($bucketName, $ossKey, $content, $options = [])
  74. {
  75. $oss = new ossServer();
  76. $oss->ossClient->setBucket($bucketName);
  77. return $oss->ossClient->uploadContent($ossKey, $content, $options);
  78. }
  79. /**
  80. * 使用阿里云内网直接上传变量内容
  81. * @param string bucket名称
  82. * @param string 上传之后的 ossServer object 名称
  83. * @param string 上传的变量
  84. * @return boolean 上传是否成功
  85. */
  86. public static function privateUploadContent($bucketName, $ossKey, $content, $options = [])
  87. {
  88. $oss = new ossServer(true);
  89. $oss->ossClient->setBucket($bucketName);
  90. return $oss->ossClient->uploadContent($ossKey, $content, $options);
  91. }
  92. /**
  93. * 使用外网删除文件
  94. * @param string bucket名称
  95. * @param string 目标 ossServer object 名称
  96. * @return boolean 删除是否成功
  97. */
  98. public static function publicDeleteObject($bucketName, $ossKey)
  99. {
  100. $oss = new ossServer();
  101. $oss->ossClient->setBucket($bucketName);
  102. return $oss->ossClient->deleteObject($bucketName, $ossKey);
  103. }
  104. /**
  105. * 使用阿里云内网删除文件
  106. * @param string bucket名称
  107. * @param string 目标 ossServer object 名称
  108. * @return boolean 删除是否成功
  109. */
  110. public static function privateDeleteObject($bucketName, $ossKey)
  111. {
  112. $oss = new ossServer(true);
  113. $oss->ossClient->setBucket($bucketName);
  114. return $oss->ossClient->deleteObject($bucketName, $ossKey);
  115. }
  116. /**
  117. * -------------------------------------------------
  118. *
  119. *
  120. * 下面不再分公网内网出 API,也不注释了,大家自行体会吧。。。
  121. *
  122. *
  123. * -------------------------------------------------
  124. */
  125. public function copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey)
  126. {
  127. $oss = new ossServer();
  128. return $oss->ossClient->copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey);
  129. }
  130. public function moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey)
  131. {
  132. $oss = new ossServer();
  133. return $oss->ossClient->moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey);
  134. }
  135. // 获取公开文件的 URL
  136. public static function getPublicObjectURL($bucketName, $ossKey)
  137. {
  138. $oss = new ossServer();
  139. $oss->ossClient->setBucket($bucketName);
  140. return $oss->ossClient->getPublicUrl($ossKey);
  141. }
  142. // 获取私有文件的URL,并设定过期时间,如 \DateTime('+1 day')
  143. public static function getPrivateObjectURLWithExpireTime($bucketName, $ossKey, DateTime $expire_time)
  144. {
  145. $oss = new ossServer();
  146. $oss->ossClient->setBucket($bucketName);
  147. return $oss->ossClient->getUrl($ossKey, $expire_time);
  148. }
  149. public static function createBucket($bucketName)
  150. {
  151. $oss = new ossServer();
  152. return $oss->ossClient->createBucket($bucketName);
  153. }
  154. public static function getAllObjectKey($bucketName)
  155. {
  156. $oss = new ossServer();
  157. return $oss->ossClient->getAllObjectKey($bucketName);
  158. }
  159. public static function getObjectMeta($bucketName, $ossKey)
  160. {
  161. $oss = new ossServer();
  162. return $oss->ossClient->getObjectMeta($bucketName, $ossKey);
  163. }
  164. }