截流自动化的商城平台
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.

Qcloud.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\common\server\storage\engine;
  3. use Qcloud\Cos\Client;
  4. /**
  5. * 腾讯云存储引擎 (COS)
  6. * Class Qiniu
  7. * @package app\common\library\storage\engine
  8. */
  9. class Qcloud extends Server
  10. {
  11. private $config;
  12. private $cosClient;
  13. /**
  14. * 构造方法
  15. * Qcloud constructor.
  16. * @param $config
  17. */
  18. public function __construct($config)
  19. {
  20. parent::__construct();
  21. $this->config = $config;
  22. // 创建COS控制类
  23. $this->createCosClient();
  24. }
  25. /**
  26. * 创建COS控制类
  27. */
  28. private function createCosClient()
  29. {
  30. $this->cosClient = new Client([
  31. 'region' => $this->config['region'],
  32. 'credentials' => [
  33. 'secretId' => $this->config['secret_id'],
  34. 'secretKey' => $this->config['secret_key'],
  35. ],
  36. ]);
  37. }
  38. /**
  39. * 执行上传
  40. * @param $save_dir (保存路径)
  41. * @return bool|mixed
  42. */
  43. public function upload($save_dir)
  44. {
  45. // 上传文件
  46. // putObject(上传接口,最大支持上传5G文件)
  47. try {
  48. $result = $this->cosClient->putObject([
  49. 'Bucket' => $this->config['bucket'],
  50. 'Key' => $save_dir . '/' . $this->fileName,
  51. 'Body' => fopen($this->getRealPath(), 'rb')
  52. ]);
  53. return true;
  54. } catch (\Exception $e) {
  55. $this->error = $e->getMessage();
  56. return false;
  57. }
  58. }
  59. /**
  60. * Notes: 抓取远程资源
  61. * @param $url
  62. * @param null $key
  63. * @author 张无忌(2021/3/2 14:36)
  64. * @return mixed|void
  65. */
  66. public function fetch($url, $key=null) {
  67. // putObject(上传接口,最大支持上传5G文件)
  68. try {
  69. $result = $this->cosClient->putObject([
  70. 'Bucket' => $this->config['bucket'],
  71. 'Key' => $key,
  72. 'Body' => fopen($url, 'rb')
  73. ]);
  74. return true;
  75. } catch (\Exception $e) {
  76. $this->error = $e->getMessage();
  77. return false;
  78. }
  79. }
  80. /**
  81. * 删除文件
  82. * @param $fileName
  83. * @return bool|mixed
  84. */
  85. public function delete($fileName)
  86. {
  87. try {
  88. $result = $this->cosClient->deleteObject(array(
  89. 'Bucket' => $this->config['bucket'],
  90. 'Key' => $fileName
  91. ));
  92. return true;
  93. } catch (\Exception $e) {
  94. $this->error = $e->getMessage();
  95. return false;
  96. }
  97. }
  98. /**
  99. * 返回文件路径
  100. * @return mixed
  101. */
  102. public function getFileName()
  103. {
  104. return $this->fileName;
  105. }
  106. }