COS.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. 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(SystemConfigService::get('accessKey'));
  36. self::$secretKey = trim(SystemConfigService::get('secretKey'));
  37. self::$uploadUrl = trim(SystemConfigService::get('uploadUrl')).'/';
  38. self::$storageName = trim(SystemConfigService::get('storage_name'));
  39. self::$storageRegion = trim(SystemConfigService::get('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. $request = app('request');
  67. $file = $request->file($filename);
  68. $filePath = $file->getRealPath();
  69. $ext = $file->getOriginalExtension();
  70. $key = substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
  71. try {
  72. self::autoInfo();
  73. return [self::$uploadUrl.$key,self::$auth->putObject([
  74. 'Bucket' => self::$storageName,
  75. 'Key' => $key,
  76. 'Body' => fopen($filePath, 'rb')
  77. ])];
  78. } catch (\Exception $e) {
  79. return [false,$e->getMessage()];
  80. }
  81. }
  82. /**
  83. * TODO 文件上传 内容
  84. * @param $key
  85. * @param $content
  86. * @return string
  87. */
  88. public static function uploadImageStream($key, $content){
  89. try {
  90. self::autoInfo();
  91. return [self::$uploadUrl.$key,self::$auth->putObject([
  92. 'Bucket' => self::$storageName,
  93. 'Key' => $key,
  94. 'Body' => $content
  95. ])];
  96. } catch (\Exception $e) {
  97. return [false,$e->getMessage()];
  98. }
  99. }
  100. /**
  101. * TODO 删除资源
  102. * @param $key
  103. * @return mixed
  104. */
  105. public static function delete($key){
  106. try {
  107. self::autoInfo();
  108. return self::$auth->deleteObject([
  109. 'Bucket' => self::$storageName,
  110. 'Key' => $key
  111. ]);
  112. } catch (\Exception $e) {
  113. return $e->getMessage();
  114. }
  115. }
  116. /**
  117. * TODO 转为文件流
  118. * @param $resource
  119. * @return EntityBody
  120. */
  121. public static function resourceStream($resource)
  122. {
  123. return EntityBody::factory($resource)->__toString();
  124. }
  125. }