COS.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace crmeb\services\storage;
  3. use crmeb\services\SystemConfigService;
  4. use Guzzle\Http\EntityBody;
  5. use Qcloud\Cos\Client;
  6. use think\facade\Cache;
  7. /**
  8. * TODO 腾讯云COS文件上传
  9. * Class COS
  10. */
  11. class COS
  12. {
  13. protected static $accessKey;
  14. protected static $secretKey;
  15. protected static $auth = null;
  16. //TODO 空间域名 Domain
  17. protected static $uploadUrl;
  18. //TODO 存储空间名称 公开空间
  19. protected static $storageName;
  20. //TODO COS使用 所属地域
  21. protected static $storageRegion;
  22. /**
  23. * TODO 初始化
  24. * @return null|Client
  25. * @throws \Exception
  26. */
  27. protected static function autoInfo()
  28. {
  29. if (($storageRegion = Cache::get('storageRegion')) && ($storageName = Cache::get('storageName')) && ($uploadUrl = Cache::get('uploadUrl')) && ($accessKey = Cache::get('accessKey')) && ($secretKey = Cache::get('secretKey'))) {
  30. self::$accessKey = $accessKey;
  31. self::$secretKey = $secretKey;
  32. self::$uploadUrl = $uploadUrl;
  33. self::$storageName = $storageName;
  34. self::$storageRegion = $storageRegion;
  35. } else {
  36. self::$accessKey = trim(sysConfig('accessKey'));
  37. self::$secretKey = trim(sysConfig('secretKey'));
  38. self::$uploadUrl = trim(sysConfig('uploadUrl')) . '/';
  39. self::$storageName = trim(sysConfig('storage_name'));
  40. self::$storageRegion = trim(sysConfig('storage_region'));
  41. Cache::set('accessKey', self::$accessKey);
  42. Cache::set('secretKey', self::$secretKey);
  43. Cache::set('uploadUrl', self::$uploadUrl);
  44. Cache::set('storageName', self::$storageName);
  45. Cache::set('storageRegion', self::$storageRegion);
  46. }
  47. if (!self::$accessKey || !self::$secretKey || !self::$uploadUrl || !self::$storageName) {
  48. exception('请设置 secretKey 和 accessKey 和 空间域名 和 存储空间名称');
  49. }
  50. if (self::$auth == null) {
  51. self::$auth = new Client([
  52. 'region' => self::$storageRegion,
  53. 'credentials' => [
  54. 'secretId' => self::$accessKey,
  55. 'secretKey' => self::$secretKey,
  56. ]
  57. ]);
  58. }
  59. return self::$auth;
  60. }
  61. /**
  62. * TODO 文件上传 名称
  63. * @param string $filename
  64. * @return string
  65. */
  66. public static function uploadImage($filename = 'image')
  67. {
  68. $request = app('request');
  69. $file = $request->file($filename);
  70. $filePath = $file->getRealPath();
  71. $ext = $file->getOriginalExtension();
  72. $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;
  73. try {
  74. self::autoInfo();
  75. return [self::$uploadUrl . $key, self::$auth->putObject([
  76. 'Bucket' => self::$storageName,
  77. 'Key' => $key,
  78. 'Body' => fopen($filePath, 'rb')
  79. ])];
  80. } catch (\Exception $e) {
  81. return [false, $e->getMessage()];
  82. }
  83. }
  84. /**
  85. * TODO 文件上传 内容
  86. * @param $key
  87. * @param $content
  88. * @return string
  89. */
  90. public static function uploadImageStream($key, $content)
  91. {
  92. try {
  93. self::autoInfo();
  94. return [self::$uploadUrl . $key, self::$auth->putObject([
  95. 'Bucket' => self::$storageName,
  96. 'Key' => $key,
  97. 'Body' => $content
  98. ])];
  99. } catch (\Exception $e) {
  100. return [false, $e->getMessage()];
  101. }
  102. }
  103. /**
  104. * TODO 删除资源
  105. * @param $key
  106. * @return mixed
  107. */
  108. public static function delete($key)
  109. {
  110. try {
  111. self::autoInfo();
  112. return self::$auth->deleteObject([
  113. 'Bucket' => self::$storageName,
  114. 'Key' => $key
  115. ]);
  116. } catch (\Exception $e) {
  117. return $e->getMessage();
  118. }
  119. }
  120. /**
  121. * TODO 转为文件流
  122. * @param $resource
  123. * @return EntityBody
  124. */
  125. public static function resourceStream($resource)
  126. {
  127. return EntityBody::factory($resource)->__toString();
  128. }
  129. }