菜谱项目

OSS.php 5.2KB

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