ReportDownload.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\library\AliyunOSS;
  4. use Obs\ObsClient;
  5. use think\Db;
  6. class ReportDownload extends Admin
  7. {
  8. // OBS配置
  9. protected $config = [
  10. 'key' => '',
  11. 'secret' => '',
  12. 'endpoint' => 'https://obs.cn-east-3.myhuaweicloud.com',
  13. 'ssl_verify' => false,
  14. ];
  15. protected $obsClient;
  16. public function _initialize()
  17. {
  18. parent::_initialize(); // TODO: Change the autogenerated stub
  19. $this->obsClient = new ObsClient($this->config);
  20. }
  21. public function index()
  22. {
  23. $list = $this->getBucketObject("njqfwlkj", "excel");
  24. $this->assign("list", $list);
  25. return $this->fetch();
  26. }
  27. /**
  28. * 获取bucket 中对象
  29. */
  30. public function getBucketObject($bucketName, $object)
  31. {
  32. try {
  33. // ## OSS
  34. $prefix = 'excel_';
  35. if (APP_STATUS != "stable"){
  36. $prefix.='dev';
  37. }else{
  38. $prefix.='stable';
  39. }
  40. $res = (new AliyunOSS())->listObjects($prefix);
  41. $result = [];
  42. if ($res != ""){
  43. $data = array_reverse($res['data']);
  44. foreach ($data as $k => $v){
  45. $result[$k] = [
  46. 'DownLoadUrl' => APK_DOWN_DOMAIN."/".$v['key'],
  47. 'LastModified' => date("Y-m-d H:i:s", strtotime($v['LastModified']))
  48. // 'LastModified' => $v['LastModified']
  49. ];
  50. }
  51. }
  52. return $result;
  53. // ## OBS
  54. // $resp = $this->obsClient->listObjects([
  55. // 'Bucket' => $bucketName,
  56. // 'Key' => $object,
  57. // 'Prefix' => $object . "/",
  58. // 'MaxKeys' => 10000
  59. // ]);
  60. // if (!empty($resp) && $resp['HttpStatusCode'] == 200) {
  61. // $result = $resp['Contents'];
  62. // $lastModified = array_column($result, "LastModified");
  63. // array_multisort($lastModified, SORT_DESC, $result);
  64. // foreach ($result as $key => $val) {
  65. // $result[$key]['LastModified'] = ;
  66. // $result[$key]['DownLoadUrl'] = APK_DOWN_DOMAIN."/" . $val['Key'];
  67. // unset($result[$key]['StorageClass'], $result[$key]['Owner']);
  68. // }
  69. // return array_slice($result,0,20);
  70. // } else {
  71. // return [];
  72. // }
  73. } catch (\Obs\Common\ObsException $obsException) {
  74. printf("ExceptionCode:%s\n", $obsException->getExceptionCode());
  75. printf("ExceptionMessage:%s\n", $obsException->getExceptionMessage());
  76. }
  77. }
  78. }