OSS.php 5.6 KB

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