1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- declare(strict_types=1);
- namespace App\Service;
- use Aws\S3\S3Client;
- use Hyperf\Di\Annotation\Inject;
- use Hyperf\HttpServer\Annotation\Controller;
- use Hyperf\HttpServer\Annotation\RequestMapping;
- use Hyperf\HttpServer\Contract\ResponseInterface;
- use function Hyperf\Support\env;
- class MinioService
- {
- /**
- * @Inject
- * @var ResponseInterface
- */
- protected $response;
- public function listBuckets()
- {
- // MinIO 配置
- $config = [
- 'version' => 'latest',
- 'region' => 'us-east-1', // 对于MinIO,这个参数通常不重要,但SDK可能需要它
- 'endpoint' => env("ENDPOINT"), // 替换为您的MinIO服务器地址
- 'use_path_style_endpoint' => true, // 对于MinIO,通常需要将此设置为true
- 'credentials' => [
- 'key' => 'Kr2gB8Lo3nRvm39naO43',
- 'secret' => '2cPcnAOs3YKiLEMonx62uRN3Ep4u1HYPeSNv1HzG',
- ],
- ];
- var_dump("配置信息:",$config);
- // 创建 MinIO 客户端
- $minioClient = new S3Client($config);
- // var_dump("连接:",$minioClient);
- try {
- // 获取所有存储桶列表
- $result = $minioClient->listBuckets();
- var_dump($result);
- // 提取存储桶名称
- $buckets = array_column($result['Buckets'], 'Name');
- // 返回响应
- return $this->response->json([
- 'success' => true,
- 'buckets' => $buckets,
- ]);
- } catch (\Aws\Exception\AwsException $e) {
- var_dump("错误:",$e->getMessage());
- // 处理AWS异常
- return $this->response->withStatus(500)->json([
- 'success' => false,
- 'error' => $e->getMessage(),
- ]);
- } catch (\Exception $e) {
- // 处理其他异常
- return $this->response->withStatus(500)->json([
- 'success' => false,
- 'error' => 'An unexpected error occurred: ' . $e->getMessage(),
- ]);
- }
- }
- }
|