OSS.php 5.8 KB

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