Kodo.Class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * 七牛对象存储
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2019/11/12
  7. * Time: 3:57 PM
  8. */
  9. namespace Util\QiNiu;
  10. use Mall\Framework\Core\ResultWrapper;
  11. use Mall\Framework\Core\ErrorCode;
  12. class Kodo
  13. {
  14. /**
  15. * 七牛密钥
  16. * @var string $secret_id
  17. */
  18. private $accessKey;
  19. /**
  20. * 七牛密钥
  21. * @var string $secret_key
  22. */
  23. private $secretKey;
  24. /**
  25. * 接口主域名
  26. */
  27. private $rsHost = 'http://rs.qiniu.com';
  28. /**
  29. *上传策略,参数规格详见
  30. * 官网文档地址https://developer.qiniu.com/kodo/manual/1206/put-policy
  31. */
  32. private static $policyFields = [
  33. 'scope',
  34. 'isPrefixalScope', // 是否以前缀为文件名
  35. 'deadline', // 上传凭证有效时间
  36. 'insertOnly', // 新增模式上传
  37. 'callbackUrl',
  38. 'callbackBody',
  39. 'callbackHost',
  40. 'callbackBodyType',
  41. 'callbackFetchKey',
  42. 'returnUrl',
  43. 'returnBody',
  44. 'endUser',
  45. 'saveKey',
  46. 'insertOnly',
  47. 'detectMime',
  48. 'mimeLimit',
  49. 'fsizeMin',
  50. 'fsizeLimit', // 限制文件上传最大值 单位byte
  51. 'persistentOps',
  52. 'persistentNotifyUrl',
  53. 'persistentPipeline',
  54. 'deleteAfterDays',
  55. 'fileType',
  56. ];
  57. public function __construct($accessKey='', $secretKey='')
  58. {
  59. $this->accessKey = $accessKey;
  60. $this->secretKey = $secretKey;
  61. }
  62. /**
  63. * 生成上传凭证
  64. * 官方文档地址:
  65. * @param string $bucket 空间名
  66. * @param string $key key标识
  67. * @return ResultWrapper
  68. */
  69. public function uploadToken($bucket, $key, $expires = 3600)
  70. {
  71. // 构造上传策略
  72. $putPolicy = [
  73. 'scope' => $bucket, // 资源名称
  74. 'deadline' => time() + $expires,
  75. 'insertOnly'=>1,
  76. ];
  77. if( !empty($key) ){
  78. $putPolicy['scope'] .= ':'.$key;
  79. }
  80. // 编码上传策略字符串
  81. $encodedPutPolicy = self::base64_urlSafeEncode(json_encode($putPolicy));
  82. // 计算签名
  83. $hmac = hash_hmac('sha1', $encodedPutPolicy, $this->secretKey, true);
  84. // 对签名结果进行编码
  85. $encodeSign = self::base64_urlSafeEncode($hmac);
  86. return ResultWrapper::success($this->accessKey.':'.$encodeSign.':'.$encodedPutPolicy);
  87. }
  88. /**
  89. * 对提供的数据进行urlsafe的base64编码。
  90. *
  91. * @param string $data 待编码的数据,一般为字符串
  92. *
  93. * @return string 编码后的字符串
  94. * @link http://developer.qiniu.com/docs/v6/api/overview/appendix.html#urlsafe-base64
  95. */
  96. public static function base64_urlSafeEncode($data)
  97. {
  98. $find = array('+', '/');
  99. $replace = array('-', '_');
  100. return str_replace($find, $replace, base64_encode($data));
  101. }
  102. /**
  103. * 对提供的urlsafe的base64编码的数据进行解码
  104. *
  105. * @param string $str 待解码的数据,一般为字符串
  106. *
  107. * @return string 解码后的字符串
  108. */
  109. public static function base64_urlSafeDecode($str)
  110. {
  111. $find = array('-', '_');
  112. $replace = array('+', '/');
  113. return base64_decode(str_replace($find, $replace, $str));
  114. }
  115. /**
  116. * 删除资源
  117. * 官网文档地址:https://developer.qiniu.com/kodo/api/1257/delete
  118. */
  119. public function delete($bucket, $key)
  120. {
  121. $entry = $bucket.':'.$key;
  122. $encodedEntryURI = self::base64_urlSafeEncode($entry);
  123. $url = $this->rsHost.'/delete/'.$encodedEntryURI;
  124. $authorization = 'QBox ' . $this->signRequest($url, '', 'application/x-www-form-urlencoded');
  125. $curl_option = [
  126. CURLOPT_HTTPHEADER => [
  127. 'Content-Type:application/x-www-form-urlencoded',
  128. 'Authorization:'.$authorization,
  129. ],
  130. ];
  131. $reponse = request($url, null, 30, true, $curl_option);
  132. if($reponse['httpcode'] != 200){
  133. return ResultWrapper::fail('请求外部系统接口报错'.$reponse['httpcode'], ErrorCode::$apiNotResult);
  134. }else{
  135. $reponseData = json_decode($reponse['content'], true);
  136. if( isset($reponseData['error']) ){
  137. return ResultWrapper::fail($reponseData['error'], ErrorCode::$apiNotResult);
  138. }
  139. return ResultWrapper::success('删除成功');
  140. }
  141. }
  142. /**
  143. * 生成管理凭证
  144. * 官网文档地址:https://developer.qiniu.com/kodo/manual/1201/access-token
  145. * @param $urlString
  146. * @param $body
  147. * @param null $contentType
  148. * @return string
  149. */
  150. public function signRequest($urlString, $body, $contentType = null)
  151. {
  152. $url = parse_url($urlString);
  153. $data = '';
  154. if (array_key_exists('path', $url)) {
  155. $data = $url['path'];
  156. }
  157. if (array_key_exists('query', $url)) {
  158. $data .= '?' . $url['query'];
  159. }
  160. $data .= "\n";
  161. if ($body !== null && $contentType === 'application/x-www-form-urlencoded') {
  162. $data .= $body;
  163. }
  164. $hmac = hash_hmac('sha1', $data, $this->secretKey, true);
  165. return $this->accessKey . ':' . self::base64_urlSafeEncode($hmac);
  166. }
  167. }