截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Aliyun.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\common\server\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. * @param $save_dir (保存路径)
  26. * @return bool|mixed
  27. */
  28. public function upload($save_dir)
  29. {
  30. try {
  31. $ossClient = new OssClient(
  32. $this->config['access_key_id'],
  33. $this->config['access_key_secret'],
  34. $this->config['domain'],
  35. true
  36. );
  37. $ossClient->uploadFile(
  38. $this->config['bucket'],
  39. $save_dir . '/' . $this->fileName,
  40. $this->getRealPath()
  41. );
  42. } catch (OssException $e) {
  43. $this->error = $e->getMessage();
  44. return false;
  45. }
  46. return true;
  47. }
  48. /**
  49. * Notes: 抓取远程资源
  50. * @param $url
  51. * @param null $key
  52. * @return mixed|void
  53. * @author 张无忌(2021/3/2 14:36)
  54. */
  55. public function fetch($url, $key = null)
  56. {
  57. try {
  58. $ossClient = new OssClient(
  59. $this->config['access_key_id'],
  60. $this->config['access_key_secret'],
  61. $this->config['domain'],
  62. true
  63. );
  64. $content = file_get_contents($url);
  65. $ossClient->putObject(
  66. $this->config['bucket'],
  67. $key,
  68. $content
  69. );
  70. } catch (OssException $e) {
  71. $this->error = $e->getMessage();
  72. return false;
  73. }
  74. return true;
  75. }
  76. /**
  77. * 删除文件
  78. * @param $fileName
  79. * @return bool|mixed
  80. */
  81. public function delete($fileName)
  82. {
  83. try {
  84. $ossClient = new OssClient(
  85. $this->config['access_key_id'],
  86. $this->config['access_key_secret'],
  87. $this->config['domain'],
  88. true
  89. );
  90. $ossClient->deleteObject($this->config['bucket'], $fileName);
  91. } catch (OssException $e) {
  92. $this->error = $e->getMessage();
  93. return false;
  94. }
  95. return true;
  96. }
  97. /**
  98. * 返回文件路径
  99. * @return mixed
  100. */
  101. public function getFileName()
  102. {
  103. return $this->fileName;
  104. }
  105. }