Aliyun.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 app\common\library\storage\FileValidate;
  14. use OSS\OssClient;
  15. use OSS\Core\OssException;
  16. use think\facade\Env;
  17. /**
  18. * 阿里云存储引擎 (OSS)
  19. * Class Aliyun
  20. * @package app\common\library\storage\engine
  21. */
  22. class Aliyun extends Basics
  23. {
  24. /**
  25. * 执行上传
  26. * @return bool|mixed
  27. */
  28. public function upload()
  29. {
  30. if (!$this->validate()) {
  31. return false;
  32. }
  33. try {
  34. $ossClient = new OssClient(
  35. $this->config['access_key_id'],
  36. $this->config['access_key_secret'],
  37. $this->config['domain'],
  38. true
  39. );
  40. $result = $ossClient->uploadFile(
  41. $this->config['bucket'],
  42. $this->getSaveFileInfo()['file_path'],
  43. $this->getRealPath()
  44. );
  45. } catch (OssException $e) {
  46. $this->error = $e->getMessage();
  47. return false;
  48. }
  49. return true;
  50. }
  51. /**
  52. * 删除文件
  53. * @param string $fileName
  54. * @return bool|mixed
  55. */
  56. public function delete(string $fileName)
  57. {
  58. try {
  59. $ossClient = new OssClient(
  60. $this->config['access_key_id'],
  61. $this->config['access_key_secret'],
  62. $this->config['domain'],
  63. true
  64. );
  65. $ossClient->deleteObject($this->config['bucket'], $fileName);
  66. } catch (OssException $e) {
  67. $this->error = $e->getMessage();
  68. return false;
  69. }
  70. return true;
  71. }
  72. /**
  73. * 下载文件
  74. * @param $object
  75. * @return false|string
  76. */
  77. public function download($object){
  78. // 下载Object到本地文件examplefile.txt,并保存到指定的本地路径中(D:\\localpath)。如果指定的本地文件存在会覆盖,不存在则新建。
  79. // 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
  80. $exts = explode('.',$object);
  81. $localfile = runtime_root_path().mt_rand(10000000,99999999).'.'.$exts[1];
  82. $options = array(
  83. OssClient::OSS_FILE_DOWNLOAD => $localfile
  84. );
  85. // 使用try catch捕获异常。如果捕获到异常,则说明下载失败;如果没有捕获到异常,则说明下载成功。
  86. try{
  87. $ossClient = new OssClient(
  88. $this->config['access_key_id'],
  89. $this->config['access_key_secret'],
  90. Env::get('CHEF.SSO_END_POINT')
  91. );
  92. $ossClient->getObject($this->config['bucket'], $object, $options);
  93. } catch(OssException $e) {
  94. //printf(__FUNCTION__ . ": FAILED\n");
  95. //printf($e->getMessage() . "\n");
  96. log_record($e->getMessage());
  97. return false;
  98. }
  99. return $localfile;
  100. }
  101. /**
  102. * 验证上传的文件
  103. * @return bool
  104. */
  105. private function validate()
  106. {
  107. $FileValidate = new FileValidate;
  108. if (!$FileValidate->check([$this->validateRuleScene => $this->file])) {
  109. $this->error = $FileValidate->getError();
  110. return false;
  111. }
  112. return true;
  113. }
  114. }