QcloudCosService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: weifeng
  5. * Date: 2020-05-11
  6. * Time: 16:18
  7. *
  8. * .--, .--,
  9. * ( ( \.---./ ) )
  10. * '.__/o o\__.'
  11. * {= ^ =}
  12. * > - <
  13. * / \
  14. * // \\
  15. * //| . |\\
  16. * "'\ /'"_.-~^`'-.
  17. * \ _ /--' `
  18. * ___)( )(___
  19. * (((__) (__))) 梦之所想,心之所向.
  20. */
  21. namespace app\common\services;
  22. use app\platform\modules\system\models\SystemSetting;
  23. class QcloudCosService
  24. {
  25. private $app_id;
  26. private $region;
  27. private $secretId;
  28. private $secretKey;
  29. private $bucket;
  30. private $cosClient;
  31. public function __construct($region = '', $secretId = '', $secretKey = '', $bucket = '', $appid = '')
  32. {
  33. $this->app_id = $appid;
  34. $this->region = $region; //地域
  35. $this->secretId = $secretId;
  36. $this->secretKey = $secretKey;
  37. $this->bucket = $bucket . '-' . $appid;
  38. $this->cosClient = CosV5Service::init($this->region, $this->secretId, $this->secretKey);
  39. }
  40. /**
  41. * 上传文件流 测试
  42. * @param $bucket
  43. * @param $key
  44. * @param $srcPath
  45. * @param $cosClient
  46. */
  47. public function uploadTest($key = '')
  48. {
  49. $file = request()->getSchemeAndHttpHost() . '/static/' . $key;
  50. try {
  51. if ($file) {
  52. $result = $this->cosClient->putObject(array(
  53. 'Bucket' => $this->bucket,
  54. 'Key' => $key,
  55. 'Body' => $this->curl_file($file),
  56. ));
  57. return true;
  58. } else {
  59. return '文件资源不存在';
  60. }
  61. } catch (\Exception $e) {
  62. \Log::error('qcloud-cos上传文件流报错', $e->getMessage());
  63. return $e->getMessage();
  64. }
  65. }
  66. /**
  67. * 上传文件流
  68. * @param $bucket
  69. * @param $key
  70. * @param $srcPath
  71. * @param $cosClient
  72. */
  73. public function upload($key = '')
  74. {
  75. if (config('app.framework') == 'platform') {
  76. if (strexists($key, '/static/upload')) {
  77. $file = request()->getSchemeAndHttpHost() . $key;
  78. } else {
  79. $file = request()->getSchemeAndHttpHost() . '/static/upload/' . $key;
  80. }
  81. } else {
  82. $file = request()->getSchemeAndHttpHost() . '/attachment/' . $key;
  83. }
  84. if (!$file) {
  85. return false;
  86. }
  87. try {
  88. $this->cosClient->putObject(array(
  89. 'Bucket' => $this->bucket,
  90. 'Key' => $key,
  91. 'Body' => $this->curl_file($file),
  92. ));
  93. } catch (\Exception $e) {
  94. \Log::error('qcloud-cos上传文件流报错', $e->getMessage());
  95. return false;
  96. }
  97. return true;
  98. }
  99. public function delete($filename)
  100. {
  101. try {
  102. $result = $this->cosClient->deleteObject(array(
  103. 'Bucket' => $this->bucket,
  104. 'Key' => $filename,
  105. ));
  106. } catch (\Exception $e) {
  107. \Log::error('qcloud-cos删除文件失败', $e->getMessage());
  108. return false;
  109. }
  110. return true;
  111. }
  112. /**
  113. * 获取上传的url
  114. * @param $bucket
  115. * @param $key
  116. * @param $cosClient
  117. * @param $expire
  118. */
  119. public function getObjUrl($key = '')
  120. {
  121. $expire = 10;
  122. try {
  123. $signedUrl = $this->cosClient->getObjectUrl($this->bucket, $key, '+'.$expire.' minutes');
  124. // 请求成功
  125. return $signedUrl;
  126. } catch (\Exception $e) {
  127. // 请求失败
  128. \Log::error('qcloud-cos读取文件报错', $e);
  129. return $e->getMessage();
  130. }
  131. }
  132. public function curl_file($url)
  133. {
  134. $ch = curl_init();
  135. $timeout = 10;
  136. curl_setopt ($ch, CURLOPT_URL, $url);
  137. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  138. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  139. $file_contents = curl_exec($ch);
  140. curl_close($ch);
  141. return $file_contents;
  142. }
  143. }