ObsService.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2023-02-03
  8. * Time: 11:20
  9. */
  10. namespace app\common\services;
  11. class ObsService
  12. {
  13. private $key;
  14. private $secret;
  15. private $endpoint;
  16. private $bucket;
  17. private $obsClient;
  18. public function __construct($key = '', $secret = '', $endpoint = '', $bucket = '')
  19. {
  20. $this->key = $key;
  21. $this->secret = $secret;
  22. $this->endpoint = $endpoint;
  23. $this->bucket = $bucket;
  24. $this->obsClient = \Obs\ObsClient::factory([
  25. 'key' => $key,
  26. 'secret' => $secret,
  27. 'endpoint' => $endpoint,
  28. ]);
  29. }
  30. public function upload($file_name = '', $test = false)
  31. {
  32. if (!$test) {
  33. if (strexists($file_name, '/static/upload')) {
  34. $file = request()->getSchemeAndHttpHost() . $file_name;
  35. } else {
  36. $file = request()->getSchemeAndHttpHost() . '/static/upload/' . $file_name;
  37. }
  38. } else {
  39. $file = request()->getSchemeAndHttpHost() . $file_name;
  40. }
  41. try {
  42. $this->obsClient->putObject(array (
  43. 'Bucket' => $this->bucket,
  44. 'Key' => $file_name,
  45. 'Body' => file_get_contents($file),
  46. ));
  47. } catch (\app\common\exceptions\ShopException $exception) {
  48. \Log::error('华为云obs上传报错', $exception->getMessage());
  49. return false;
  50. }
  51. return true;
  52. }
  53. public function delete($file_name = '')
  54. {
  55. try {
  56. $this->obsClient->deleteObject(array (
  57. 'Bucket' => $this->bucket,
  58. 'Key' => $file_name,
  59. ));
  60. } catch (\app\common\exceptions\ShopException $exception) {
  61. \Log::error('华为云obs删除报错', $exception->getMessage());
  62. return false;
  63. }
  64. return true;
  65. }
  66. }