|
@@ -10,6 +10,7 @@ use Hyperf\HttpServer\Annotation\Controller;
|
|
|
use Hyperf\HttpServer\Annotation\RequestMapping;
|
|
|
use Hyperf\HttpServer\Contract\ResponseInterface;
|
|
|
use function Hyperf\Support\env;
|
|
|
+use App\Tools\Result;
|
|
|
class MinioService
|
|
|
{
|
|
|
/**
|
|
@@ -18,48 +19,46 @@ class MinioService
|
|
|
*/
|
|
|
protected $response;
|
|
|
|
|
|
- public function listBuckets()
|
|
|
+ /**
|
|
|
+ * @Inject
|
|
|
+ * @var \Hyperf\Config\ConfigInterface
|
|
|
+ */
|
|
|
+ protected $config;
|
|
|
+
|
|
|
+ protected $s3Client;
|
|
|
+
|
|
|
+ public function __construct()
|
|
|
{
|
|
|
- // MinIO 配置
|
|
|
- $config = [
|
|
|
+ $this->s3Client = new S3Client([
|
|
|
'version' => 'latest',
|
|
|
- 'region' => 'us-east-1', // 对于MinIO,这个参数通常不重要,但SDK可能需要它
|
|
|
- 'endpoint' => env("ENDPOINT"), // 替换为您的MinIO服务器地址
|
|
|
- 'use_path_style_endpoint' => true, // 对于MinIO,通常需要将此设置为true
|
|
|
+ 'region' => 'chinese',
|
|
|
+ 'endpoint' => env("OSS_ENDPOINT"),
|
|
|
+ 'use_path_style_endpoint' => true,
|
|
|
'credentials' => [
|
|
|
- 'key' => 'Kr2gB8Lo3nRvm39naO43',
|
|
|
- 'secret' => '2cPcnAOs3YKiLEMonx62uRN3Ep4u1HYPeSNv1HzG',
|
|
|
+ 'key' => env("OSS_KEY"),
|
|
|
+ 'secret' => env("OSS_SECRET"),
|
|
|
],
|
|
|
- ];
|
|
|
- var_dump("配置信息:",$config);
|
|
|
- // 创建 MinIO 客户端
|
|
|
- $minioClient = new S3Client($config);
|
|
|
-// var_dump("连接:",$minioClient);
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 没几把鸟用用来测试的
|
|
|
+ * @return \Psr\Http\Message\ResponseInterface
|
|
|
+ */
|
|
|
+ public function listBuckets()
|
|
|
+ {
|
|
|
try {
|
|
|
// 获取所有存储桶列表
|
|
|
- $result = $minioClient->listBuckets();
|
|
|
- var_dump($result);
|
|
|
+ $result = $this->s3Client->listBuckets();
|
|
|
// 提取存储桶名称
|
|
|
$buckets = array_column($result['Buckets'], 'Name');
|
|
|
-
|
|
|
- // 返回响应
|
|
|
- return $this->response->json([
|
|
|
- 'success' => true,
|
|
|
- 'buckets' => $buckets,
|
|
|
- ]);
|
|
|
+ return Result::success($buckets);
|
|
|
} catch (\Aws\Exception\AwsException $e) {
|
|
|
- var_dump("错误:",$e->getMessage());
|
|
|
- // 处理AWS异常
|
|
|
- return $this->response->withStatus(500)->json([
|
|
|
- 'success' => false,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- ]);
|
|
|
+ var_dump($e->getMessage());
|
|
|
+ return Result::error("报错了");
|
|
|
} catch (\Exception $e) {
|
|
|
- // 处理其他异常
|
|
|
- return $this->response->withStatus(500)->json([
|
|
|
- 'success' => false,
|
|
|
- 'error' => 'An unexpected error occurred: ' . $e->getMessage(),
|
|
|
- ]);
|
|
|
+ var_dump($e->getMessage());
|
|
|
+ return Result::error("报错了");
|
|
|
}
|
|
|
}
|
|
|
}
|