Aliyun.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\storage\engine;
  13. use OSS\OssClient;
  14. use OSS\Core\OssException;
  15. /**
  16. * 阿里云存储引擎 (OSS)
  17. * Class Aliyun
  18. * @package app\common\library\storage\engine
  19. */
  20. class Aliyun extends Basics
  21. {
  22. /**
  23. * 执行上传
  24. * @return bool
  25. */
  26. public function upload(): bool
  27. {
  28. try {
  29. $ossClient = new OssClient(
  30. $this->config['access_key_id'],
  31. $this->config['access_key_secret'],
  32. $this->config['domain'],
  33. true
  34. );
  35. $result = $ossClient->uploadFile(
  36. $this->config['bucket'],
  37. $this->getSaveFileInfo()['file_path'],
  38. $this->getRealPath()
  39. );
  40. } catch (OssException $e) {
  41. $this->error = $e->getMessage();
  42. return false;
  43. }
  44. return true;
  45. }
  46. /**
  47. * 删除文件
  48. * @param string $filePath
  49. * @return bool
  50. */
  51. public function delete(string $filePath): bool
  52. {
  53. try {
  54. $ossClient = new OssClient(
  55. $this->config['access_key_id'],
  56. $this->config['access_key_secret'],
  57. $this->config['domain'],
  58. true
  59. );
  60. $ossClient->deleteObject($this->config['bucket'], $filePath);
  61. } catch (OssException $e) {
  62. $this->error = $e->getMessage();
  63. return false;
  64. }
  65. return true;
  66. }
  67. }