OssStatic.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\common\command;
  12. use EasyAdmin\console\CliEcho;
  13. use EasyAdmin\tool\CommonTool;
  14. use EasyAdmin\upload\driver\alioss\Oss;
  15. use think\console\Command;
  16. use think\console\Input;
  17. use think\console\input\Option;
  18. use think\console\Output;
  19. class OssStatic extends Command
  20. {
  21. protected function configure()
  22. {
  23. $this->setName('OssStatic')
  24. ->setDescription('将静态资源上传到oss上');
  25. }
  26. protected function execute(Input $input, Output $output)
  27. {
  28. $output->writeln("========正在上传静态资源到OSS上:========" . date('Y-m-d H:i:s'));
  29. $dir = root_path() . 'public' . DIRECTORY_SEPARATOR . 'static';
  30. $list = CommonTool::readDirAllFiles($dir);
  31. $uploadConfig = sysconfig('upload');
  32. $uploadPrefix = config('app.oss_static_prefix', 'oss_static_prefix');
  33. foreach ($list as $key => $val) {
  34. list($objectName, $filePath) = [$uploadPrefix . DIRECTORY_SEPARATOR . $key, $val];
  35. try {
  36. $upload = Oss::instance($uploadConfig)
  37. ->save($objectName, $filePath);
  38. } catch (\Exception $e) {
  39. CliEcho::error('文件上传失败:' . $filePath . '。错误信息:' . $e->getMessage());
  40. continue;
  41. }
  42. if ($upload['save'] == true) {
  43. CliEcho::success('文件上传成功:' . $filePath . '。上传地址:' . $upload['url']);
  44. } else {
  45. CliEcho::error('文件上传失败:' . $filePath . '。错误信息:' . $upload['msg']);
  46. }
  47. }
  48. $output->writeln("========已完成静态资源上传到OSS上:========" . date('Y-m-d H:i:s'));
  49. }
  50. }