COS.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Api\Storage\COS;
  3. vendor('cos-php-sdk-v5.vendor.autoload');
  4. use app\core\util\SystemConfigService;
  5. use Guzzle\Http\EntityBody;
  6. use Qcloud\Cos\Client;
  7. use think\Request;
  8. use think\Cache;
  9. /**
  10. * TODO 腾讯云COS文件上传
  11. * Class COS
  12. * @package Api\Storage\COS
  13. */
  14. class COS
  15. {
  16. protected static $accessKey;
  17. protected static $secretKey;
  18. protected static $auth = null;
  19. //TODO 空间域名 Domain
  20. protected static $uploadUrl;
  21. //TODO 存储空间名称 公开空间
  22. protected static $storageName;
  23. //TODO COS使用 所属地域
  24. protected static $storageRegion;
  25. /**
  26. * TODO 初始化
  27. * @return null|Client
  28. * @throws \Exception
  29. */
  30. protected static function autoInfo(){
  31. if(($storageRegion = Cache::get('storageRegion')) && ($storageName = Cache::get('storageName')) && ($uploadUrl = Cache::get('uploadUrl')) && ($accessKey = Cache::get('accessKey')) && ($secretKey = Cache::get('secretKey'))){
  32. self::$accessKey = $accessKey;
  33. self::$secretKey = $secretKey;
  34. self::$uploadUrl = $uploadUrl;
  35. self::$storageName = $storageName;
  36. self::$storageRegion = $storageRegion;
  37. }else{
  38. self::$accessKey = trim(SystemConfigService::get('accessKey'));
  39. self::$secretKey = trim(SystemConfigService::get('secretKey'));
  40. self::$uploadUrl = trim(SystemConfigService::get('uploadUrl')).'/';
  41. self::$storageName = trim(SystemConfigService::get('storage_name'));
  42. self::$storageRegion = trim(SystemConfigService::get('storage_region'));
  43. Cache::set('accessKey',self::$accessKey);
  44. Cache::set('secretKey',self::$secretKey);
  45. Cache::set('uploadUrl',self::$uploadUrl);
  46. Cache::set('storageName',self::$storageName);
  47. Cache::set('storageRegion',self::$storageRegion);
  48. }
  49. if(!self::$accessKey || !self::$secretKey || !self::$uploadUrl || !self::$storageName){
  50. exception('请设置 secretKey 和 accessKey 和 空间域名 和 存储空间名称');
  51. }
  52. if(self::$auth == null) {
  53. self::$auth = new Client([
  54. 'region'=>self::$storageRegion,
  55. 'credentials'=>[
  56. 'secretId'=>self::$accessKey,
  57. 'secretKey'=>self::$secretKey,
  58. ]
  59. ]);
  60. }
  61. return self::$auth;
  62. }
  63. /**
  64. * TODO 文件上传 名称
  65. * @param string $filename
  66. * @return string
  67. */
  68. public static function uploadImage($filename = 'image'){
  69. $request = Request::instance();
  70. $file = $request->file($filename);
  71. $filePath = $file->getRealPath();
  72. $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  73. $key = substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
  74. try {
  75. self::autoInfo();
  76. return [self::$uploadUrl.$key,self::$auth->putObject([
  77. 'Bucket' => self::$storageName,
  78. 'Key' => $key,
  79. 'Body' => fopen($filePath, 'rb')
  80. ])];
  81. } catch (\Exception $e) {
  82. return [false,$e->getMessage()];
  83. }
  84. }
  85. /**
  86. * TODO 文件上传 内容
  87. * @param $key
  88. * @param $content
  89. * @return string
  90. */
  91. public static function uploadImageStream($key, $content){
  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. 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. }