Cos.php 11 KB

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