Aliyun.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 OSS\Core\OssException;
  10. use OSS\OssClient;
  11. class Aliyun extends BaseDriver
  12. {
  13. public $isCName = false;
  14. public $endPoint = '';
  15. /**
  16. * Aliyun OSS Client
  17. *
  18. * @var OssClient
  19. */
  20. protected $ossClient;
  21. public function __construct($config = [])
  22. {
  23. parent::__construct($config);
  24. $this->ossClient = new OssClient(
  25. $this->accessKey,
  26. $this->secretKey,
  27. $this->endPoint,
  28. $this->isCName
  29. );
  30. }
  31. public function put($localFile, $saveTo)
  32. {
  33. try {
  34. $res = $this->ossClient->uploadFile($this->bucket, $saveTo, $localFile);
  35. } catch (OssException $ex) {
  36. throw new StorageException($ex->getErrorMessage() ?: $ex->getMessage());
  37. }
  38. return $res['oss-request-url'];
  39. }
  40. }