Qcloud.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\librarys\storage\drivers;
  8. use app\librarys\storage\exceptions\StorageException;
  9. use Qcloud\Cos\Client as CosClient;
  10. class Qcloud extends BaseDriver
  11. {
  12. public $region;
  13. public $appId;
  14. /**
  15. * Tencent cloud COS Client
  16. *
  17. * @var CosClient
  18. */
  19. protected $cosClient;
  20. public function __construct($config = [])
  21. {
  22. parent::__construct($config);
  23. $this->cosClient = new CosClient(
  24. [
  25. 'region' => $this->region,
  26. 'credentials' => [
  27. 'appId' => $this->appId,
  28. 'secretId' => $this->accessKey,
  29. 'secretKey' => $this->secretKey,
  30. ],
  31. ]
  32. );
  33. }
  34. public function put($localFile, $saveTo)
  35. {
  36. $handle = fopen($localFile, 'rb');
  37. try {
  38. $saveTo = ltrim($saveTo, '/');
  39. $res = $this->cosClient->putObject(
  40. [
  41. 'Bucket' => $this->bucket,
  42. 'Key' => $saveTo,
  43. 'Body' => $handle,
  44. ]
  45. );
  46. } catch (\Exception $ex) {
  47. throw new StorageException($ex->getMessage());
  48. }
  49. return $res->offsetGet('Location');
  50. }
  51. }