Cos.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace crmeb\services\upload\storage;
  3. use crmeb\basic\BaseUpload;
  4. use crmeb\exceptions\UploadException;
  5. use Guzzle\Http\EntityBody;
  6. use Qcloud\Cos\Client;
  7. use think\exception\ValidateException;
  8. /**
  9. * 腾讯云COS文件上传
  10. * Class COS
  11. * @package crmeb\services\upload\storage
  12. */
  13. class Cos extends BaseUpload
  14. {
  15. /**
  16. * accessKey
  17. * @var mixed
  18. */
  19. protected $accessKey;
  20. /**
  21. * secretKey
  22. * @var mixed
  23. */
  24. protected $secretKey;
  25. /**
  26. * 句柄
  27. * @var Client
  28. */
  29. protected $handle;
  30. /**
  31. * 空间域名 Domain
  32. * @var mixed
  33. */
  34. protected $uploadUrl;
  35. /**
  36. * 存储空间名称 公开空间
  37. * @var mixed
  38. */
  39. protected $storageName;
  40. /**
  41. * COS使用 所属地域
  42. * @var mixed|null
  43. */
  44. protected $storageRegion;
  45. /**
  46. * 初始化
  47. * @param array $config
  48. * @return mixed|void
  49. */
  50. public function initialize(array $config)
  51. {
  52. parent::initialize($config);
  53. $this->accessKey = $config['accessKey'] ?? null;
  54. $this->secretKey = $config['secretKey'] ?? null;
  55. $this->uploadUrl = $this->checkUploadUrl($config['uploadUrl'] ?? '');
  56. $this->storageName = $config['storageName'] ?? null;
  57. $this->storageRegion = $config['storageRegion'] ?? null;
  58. }
  59. /**
  60. * 实例化cos
  61. * @return Client
  62. */
  63. protected function app()
  64. {
  65. if (!$this->accessKey || !$this->secretKey) {
  66. throw new UploadException('Please configure accessKey and secretKey');
  67. }
  68. $this->handle = new Client(['region' => $this->storageRegion, 'credentials' => [
  69. 'secretId' => $this->accessKey, 'secretKey' => $this->secretKey
  70. ]]);
  71. return $this->handle;
  72. }
  73. /**
  74. * 上传文件
  75. * @param string|null $file
  76. * @param bool $isStream 是否为流上传
  77. * @param string|null $fileContent 流内容
  78. * @return array|bool|\StdClass
  79. */
  80. protected function upload(string $file = null, bool $isStream = false, string $fileContent = null)
  81. {
  82. if (!$isStream) {
  83. $fileHandle = app()->request->file($file);
  84. if (!$fileHandle) {
  85. return $this->setError('Upload file does not exist');
  86. }
  87. if ($this->validate) {
  88. try {
  89. validate([$file => $this->validate])->check([$file => $fileHandle]);
  90. } catch (ValidateException $e) {
  91. return $this->setError($e->getMessage());
  92. }
  93. }
  94. $key = $this->saveFileName($fileHandle->getRealPath(), $fileHandle->getOriginalExtension());
  95. $body = fopen($fileHandle->getRealPath(), 'rb');
  96. } else {
  97. $key = $file;
  98. $body = $fileContent;
  99. }
  100. try {
  101. $this->fileInfo->uploadInfo = $this->app()->putObject([
  102. 'Bucket' => $this->storageName,
  103. 'Key' => $key,
  104. 'Body' => $body
  105. ]);
  106. $this->fileInfo->filePath = $this->uploadUrl . '/' . $key;
  107. $this->fileInfo->fileName = $key;
  108. return $this->fileInfo;
  109. } catch (UploadException $e) {
  110. return $this->setError($e->getMessage());
  111. }
  112. }
  113. /**
  114. * 文件流上传
  115. * @param string $fileContent
  116. * @param string|null $key
  117. * @return array|bool|mixed|\StdClass
  118. */
  119. public function stream(string $fileContent, string $key = null)
  120. {
  121. if (!$key) {
  122. $key = $this->saveFileName();
  123. }
  124. return $this->upload($key, true, $fileContent);
  125. }
  126. /**
  127. * 文件上传
  128. * @param string $file
  129. * @return array|bool|mixed|\StdClass
  130. */
  131. public function move(string $file = 'file')
  132. {
  133. return $this->upload($file);
  134. }
  135. /**
  136. * TODO 删除资源
  137. * @param $key
  138. * @return mixed
  139. */
  140. public function delete(string $filePath)
  141. {
  142. try {
  143. return $this->app()->deleteObject(['Bucket' => $this->storageName, 'Key' => $filePath]);
  144. } catch (\Exception $e) {
  145. return $this->setError($e->getMessage());
  146. }
  147. }
  148. }