MinioService.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service;
  4. use Aws\S3\S3Client;
  5. use Hyperf\Di\Annotation\Inject;
  6. use Hyperf\HttpServer\Annotation\Controller;
  7. use Hyperf\HttpServer\Annotation\RequestMapping;
  8. use Hyperf\HttpServer\Contract\ResponseInterface;
  9. use function Hyperf\Support\env;
  10. use App\Tools\Result;
  11. class MinioService
  12. {
  13. /**
  14. * @Inject
  15. * @var ResponseInterface
  16. */
  17. protected $response;
  18. /**
  19. * @Inject
  20. * @var \Hyperf\Config\ConfigInterface
  21. */
  22. protected $config;
  23. protected $s3Client;
  24. public function __construct()
  25. {
  26. $this->s3Client = new S3Client([
  27. 'version' => 'latest',
  28. 'region' => 'chinese',
  29. 'endpoint' => env("OSS_ENDPOINT"),
  30. 'use_path_style_endpoint' => true,
  31. 'credentials' => [
  32. 'key' => env("OSS_KEY"),
  33. 'secret' => env("OSS_SECRET"),
  34. ],
  35. ]);
  36. }
  37. /**
  38. * 没几把鸟用用来测试的
  39. * @return \Psr\Http\Message\ResponseInterface
  40. */
  41. public function listBuckets()
  42. {
  43. try {
  44. // 获取所有存储桶列表
  45. $result = $this->s3Client->listBuckets();
  46. // 提取存储桶名称
  47. $buckets = array_column($result['Buckets'], 'Name');
  48. return Result::success($buckets);
  49. } catch (\Aws\Exception\AwsException $e) {
  50. var_dump($e->getMessage());
  51. return Result::error("报错了");
  52. } catch (\Exception $e) {
  53. var_dump($e->getMessage());
  54. return Result::error("报错了");
  55. }
  56. }
  57. }