Aliyun.php 1.8 KB

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