COS.php 4.1 KB

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