Aliyun.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\library\storage\engine;
  10. use OSS\OssClient;
  11. use OSS\Core\OssException;
  12. /**
  13. * 阿里云存储引擎 (OSS)
  14. * Class Qiniu
  15. * @package app\common\library\storage\engine
  16. */
  17. class Aliyun extends Server
  18. {
  19. private $config;
  20. /**
  21. * 构造方法
  22. * Aliyun constructor.
  23. * @param $config
  24. */
  25. public function __construct($config)
  26. {
  27. parent::__construct();
  28. $this->config = $config;
  29. }
  30. /**
  31. * 执行上传
  32. * @return bool|mixed
  33. */
  34. public function upload()
  35. {
  36. try {
  37. $ossClient = new OssClient(
  38. $this->config['access_key_id'],
  39. $this->config['access_key_secret'],
  40. $this->config['domain'],
  41. true
  42. );
  43. $result = $ossClient->uploadFile(
  44. $this->config['bucket'],
  45. $this->fileName,
  46. $this->getRealPath()
  47. );
  48. } catch (OssException $e) {
  49. $this->error = $e->getMessage();
  50. return false;
  51. }
  52. return true;
  53. }
  54. /**
  55. * 删除文件
  56. * @param $fileName
  57. * @return bool|mixed
  58. */
  59. public function delete($fileName)
  60. {
  61. try {
  62. $ossClient = new OssClient(
  63. $this->config['access_key_id'],
  64. $this->config['access_key_secret'],
  65. $this->config['domain'],
  66. true
  67. );
  68. $ossClient->deleteObject($this->config['bucket'], $fileName);
  69. } catch (OssException $e) {
  70. $this->error = $e->getMessage();
  71. return false;
  72. }
  73. return true;
  74. }
  75. /**
  76. * 返回文件路径
  77. * @return mixed
  78. */
  79. public function getFileName()
  80. {
  81. return $this->fileName;
  82. }
  83. }