Google.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\storage\engine;
  13. use Google\Cloud\Storage\StorageClient;
  14. use think\facade\Log;
  15. /**
  16. * 谷歌存储引擎 (GCS)
  17. * Class Google
  18. * @package app\common\library\storage\engine
  19. */
  20. class Google extends Basics
  21. {
  22. /**
  23. * 执行上传
  24. * @return bool
  25. */
  26. public function upload(): bool
  27. {
  28. $keyFilePath = '/etc/nginx/nginx/html/dihua-01-5690813badec.json';
  29. $storage = new StorageClient(['projectId' => 'dihua-01', 'keyFilePath' => $keyFilePath]);
  30. $bucket = $storage->bucket('freeshippingvps');
  31. $filePathAbsolute = public_path('uploads') . $this->getSaveFileInfo()['file_path'];
  32. try {
  33. $storageObj = $bucket->upload(
  34. fopen($filePathAbsolute, 'r'),
  35. [
  36. 'name' => $this->getSaveFileInfo()['file_path']
  37. ]
  38. );
  39. $name = $storageObj->name();
  40. if (empty($name)) {
  41. Log::error('上传文件到google云失败');
  42. $this->error = '上传文件到google云失败';
  43. return false;
  44. }
  45. } catch (\Exception $e) {
  46. $this->error = $e->getMessage();
  47. return false;
  48. }
  49. return true;
  50. }
  51. /**
  52. * 删除文件
  53. * @param string $filePath
  54. * @return bool
  55. */
  56. public function delete(string $filePath): bool
  57. {
  58. return true;
  59. }
  60. }