Cos.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\upload\storage;
  12. use crmeb\basic\BaseUpload;
  13. use crmeb\exceptions\UploadException;
  14. use Guzzle\Http\EntityBody;
  15. use Qcloud\Cos\Client;
  16. use think\Exception;
  17. use think\exception\ValidateException;
  18. /**
  19. * 腾讯云COS文件上传
  20. * Class COS
  21. * @package crmeb\services\upload\storage
  22. */
  23. class Cos extends BaseUpload
  24. {
  25. /**
  26. * accessKey
  27. * @var mixed
  28. */
  29. protected $accessKey;
  30. /**
  31. * secretKey
  32. * @var mixed
  33. */
  34. protected $secretKey;
  35. /**
  36. * 句柄
  37. * @var Client
  38. */
  39. protected $handle;
  40. /**
  41. * 空间域名 Domain
  42. * @var mixed
  43. */
  44. protected $uploadUrl;
  45. /**
  46. * 存储空间名称 公开空间
  47. * @var mixed
  48. */
  49. protected $storageName;
  50. /**
  51. * COS使用 所属地域
  52. * @var mixed|null
  53. */
  54. protected $storageRegion;
  55. /**
  56. * 初始化
  57. * @param array $config
  58. * @return mixed|void
  59. */
  60. public function initialize(array $config)
  61. {
  62. parent::initialize($config);
  63. $this->accessKey = $config['accessKey'] ?? null;
  64. $this->secretKey = $config['secretKey'] ?? null;
  65. $this->uploadUrl = $this->checkUploadUrl($config['uploadUrl'] ?? '');
  66. $this->storageName = $config['storageName'] ?? null;
  67. $this->storageRegion = $config['storageRegion'] ?? null;
  68. }
  69. /**
  70. * 实例化cos
  71. * @return Client
  72. */
  73. protected function app()
  74. {
  75. if (!$this->accessKey || !$this->secretKey) {
  76. throw new UploadException('Please configure accessKey and secretKey');
  77. }
  78. $this->handle = new Client(['region' => $this->storageRegion, 'credentials' => [
  79. 'secretId' => $this->accessKey, 'secretKey' => $this->secretKey
  80. ]]);
  81. return $this->handle;
  82. }
  83. /**
  84. * 上传文件
  85. * @param string|null $file
  86. * @param bool $isStream 是否为流上传
  87. * @param string|null $fileContent 流内容
  88. * @return array|bool|\StdClass
  89. */
  90. protected function upload(string $file = null, bool $isStream = false, string $fileContent = null)
  91. {
  92. if (!$isStream) {
  93. $fileHandle = app()->request->file($file);
  94. if (!$fileHandle) {
  95. return $this->setError('Upload file does not exist');
  96. }
  97. if ($this->validate) {
  98. try {
  99. validate([$file => $this->validate])->check([$file => $fileHandle]);
  100. } catch (ValidateException $e) {
  101. return $this->setError($e->getMessage());
  102. }
  103. }
  104. $key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension());
  105. $body = fopen($fileHandle->getRealPath(), 'rb');
  106. } else {
  107. $key = $file;
  108. $body = $fileContent;
  109. }
  110. try {
  111. $this->fileInfo->uploadInfo = $this->app()->putObject([
  112. 'Bucket' => $this->storageName,
  113. 'Key' => $key,
  114. 'Body' => $body
  115. ]);
  116. $this->fileInfo->filePath = $this->uploadUrl . '/' . $key;
  117. $this->fileInfo->fileName = $key;
  118. return $this->fileInfo;
  119. } catch (UploadException $e) {
  120. return $this->setError($e->getMessage());
  121. }
  122. }
  123. /**
  124. * 文件流上传
  125. * @param string $fileContent
  126. * @param string|null $key
  127. * @return array|bool|mixed|\StdClass
  128. */
  129. public function stream(string $fileContent, string $key = null)
  130. {
  131. if (!$key) {
  132. $key = $this->saveFileName();
  133. }
  134. return $this->upload($key, true, $fileContent);
  135. }
  136. /**
  137. * 文件上传
  138. * @param string $file
  139. * @return array|bool|mixed|\StdClass
  140. */
  141. public function move(string $file = 'file')
  142. {
  143. return $this->upload($file);
  144. }
  145. /**
  146. * TODO 删除资源
  147. * @param $key
  148. * @return mixed
  149. */
  150. public function delete(string $filePath)
  151. {
  152. try {
  153. return $this->app()->deleteObject(['Bucket' => $this->storageName, 'Key' => $filePath]);
  154. } catch (\Exception $e) {
  155. return $this->setError($e->getMessage());
  156. }
  157. }
  158. /**
  159. * 获取腾讯云存储临时密钥
  160. * @return array|bool|mixed|null|string
  161. */
  162. public function getTempKeys()
  163. {
  164. // TODO: Implement getTempKeys() method.
  165. $config = array(
  166. 'url' => 'https://sts.tencentcloudapi.com/',
  167. 'domain' => 'sts.tencentcloudapi.com',
  168. 'proxy' => '',
  169. 'secretId' => $this->accessKey, // 固定密钥
  170. 'secretKey' => $this->secretKey, // 固定密钥
  171. 'bucket' => $this->storageName, // 换成你的 bucket
  172. 'region' => $this->storageRegion, // 换成 bucket 所在园区
  173. 'durationSeconds' => 1800, // 密钥有效期
  174. 'allowPrefix' => '*', // 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
  175. // 密钥的权限列表。简单上传和分片需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
  176. 'allowActions' => array (
  177. // 简单上传
  178. 'name/cos:PutObject',
  179. 'name/cos:PostObject',
  180. // 分片上传
  181. 'name/cos:InitiateMultipartUpload',
  182. 'name/cos:ListMultipartUploads',
  183. 'name/cos:ListParts',
  184. 'name/cos:UploadPart',
  185. 'name/cos:CompleteMultipartUpload'
  186. )
  187. );
  188. $result = null;
  189. try{
  190. if(array_key_exists('policy', $config)){
  191. $policy = $config['policy'];
  192. }else{
  193. if(array_key_exists('bucket', $config)){
  194. $ShortBucketName = substr($config['bucket'],0, strripos($config['bucket'], '-'));
  195. $AppId = substr($config['bucket'], 1 + strripos($config['bucket'], '-'));
  196. }else{
  197. throw new Exception("bucket== null");
  198. }
  199. if(array_key_exists('allowPrefix', $config)){
  200. if(!(strpos($config['allowPrefix'], '/') === 0)){
  201. $config['allowPrefix'] = '/' . $config['allowPrefix'];
  202. }
  203. }else{
  204. throw new Exception("allowPrefix == null");
  205. }
  206. $policy = array(
  207. 'version'=> '2.0',
  208. 'statement'=> array(
  209. array(
  210. 'action'=> $config['allowActions'],
  211. 'effect'=> 'allow',
  212. 'principal'=> array('qcs'=> array('*')),
  213. 'resource'=> array(
  214. 'qcs::cos:' . $config['region'] . ':uid/' . $AppId . ':' . $config['bucket'] . $config['allowPrefix']
  215. )
  216. )
  217. )
  218. );
  219. }
  220. $policyStr = str_replace('\\/', '/', json_encode($policy));
  221. $Action = 'GetFederationToken';
  222. $Nonce = rand(10000, 20000);
  223. $Timestamp = time();
  224. $Method = 'POST';
  225. if(array_key_exists('durationSeconds', $config)){
  226. if(!(is_integer($config['durationSeconds']))){
  227. throw new exception("durationSeconds must be a int type");
  228. }
  229. }
  230. $params = array(
  231. 'SecretId'=> $config['secretId'],
  232. 'Timestamp'=> $Timestamp,
  233. 'Nonce'=> $Nonce,
  234. 'Action'=> $Action,
  235. 'DurationSeconds'=> $config['durationSeconds'],
  236. 'Version'=>'2018-08-13',
  237. 'Name'=> 'cos',
  238. 'Region'=> $config['region'],
  239. 'Policy'=> urlencode($policyStr)
  240. );
  241. $params['Signature'] = $this->getSignature($params, $config['secretKey'], $Method, $config);
  242. $url = $config['url'];
  243. $ch = curl_init($url);
  244. if(array_key_exists('proxy', $config)){
  245. $config['proxy'] && curl_setopt($ch, CURLOPT_PROXY, $config['proxy']);
  246. }
  247. curl_setopt($ch, CURLOPT_HEADER, 0);
  248. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
  249. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
  250. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  251. curl_setopt($ch, CURLOPT_POST, 1);
  252. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->json2str($params));
  253. $result = curl_exec($ch);
  254. if(curl_errno($ch)) $result = curl_error($ch);
  255. curl_close($ch);
  256. $result = json_decode($result, 1);
  257. if (isset($result['Response'])) {
  258. $result = $result['Response'];
  259. if(isset($result['Error'])){
  260. throw new Exception("get cam failed");
  261. }
  262. $result['startTime'] = $result['ExpiredTime'] - $config['durationSeconds'];
  263. }
  264. $result = $this->backwardCompat($result);
  265. $result['url'] = $this->uploadUrl.'/';
  266. $result['type'] = 'COS';
  267. return $result;
  268. }catch(Exception $e){
  269. if($result == null){
  270. $result = "error: " . + $e->getMessage();
  271. }else{
  272. $result = json_encode($result);
  273. }
  274. throw new Exception($result);
  275. }
  276. }
  277. /**
  278. * 计算临时密钥用的签名
  279. * @param $opt
  280. * @param $key
  281. * @param $method
  282. * @param $config
  283. * @return string
  284. */
  285. public function getSignature($opt, $key, $method, $config) {
  286. $formatString = $method . $config['domain'] . '/?' . $this->json2str($opt, 1);
  287. $sign = hash_hmac('sha1', $formatString, $key);
  288. $sign = base64_encode($this->_hex2bin($sign));
  289. return $sign;
  290. }
  291. public function _hex2bin($data) {
  292. $len = strlen($data);
  293. return pack("H" . $len, $data);
  294. }
  295. // obj 转 query string
  296. public function json2str($obj, $notEncode = false) {
  297. ksort($obj);
  298. $arr = array();
  299. if(!is_array($obj)){
  300. return $this->setError($obj . " must be a array");
  301. }
  302. foreach ($obj as $key => $val) {
  303. array_push($arr, $key . '=' . ($notEncode ? $val : rawurlencode($val)));
  304. }
  305. return join('&', $arr);
  306. }
  307. // v2接口的key首字母小写,v3改成大写,此处做了向下兼容
  308. public function backwardCompat($result) {
  309. if(!is_array($result)){
  310. return $this->setError($result . " must be a array");
  311. }
  312. $compat = array();
  313. foreach ($result as $key => $value) {
  314. if(is_array($value)) {
  315. $compat[lcfirst($key)] = $this->backwardCompat($value);
  316. } elseif ($key == 'Token') {
  317. $compat['sessionToken'] = $value;
  318. } else {
  319. $compat[lcfirst($key)] = $value;
  320. }
  321. }
  322. return $compat;
  323. }
  324. }