Quellcode durchsuchen

先读取oss的bucket列表

rkljw vor 4 Monaten
Ursprung
Commit
db43fec493
3 geänderte Dateien mit 39 neuen und 40 gelöschten Zeilen
  1. 4 2
      .env
  2. 4 6
      app/Controller/MinioController.php
  3. 31 32
      app/Service/MinioService.php

+ 4 - 2
.env

@@ -35,5 +35,7 @@ WECHAT = https://api.weixin.qq.com/
 APPID = wx830ada852dd1707f
 #小程序SECRET
 APP_SECRET = 09d1ac9287cb6f3c5e81aa27a6b7138e
-#12
-ENDPOINT =http://123.57.1.253:9527
+#图片服务器地址
+OSS_ENDPOINT =http://116.131.8.26:19000
+OSS_KEY = mvZzLn24wFzp4Vhj1jPM
+OSS_SECRET = EHCS83TtSGNxkihy8a5gm6wSC4sKYVECszIUWe3a

+ 4 - 6
app/Controller/MinioController.php

@@ -9,7 +9,7 @@ use Hyperf\HttpServer\Annotation\Controller;
 use Hyperf\HttpServer\Annotation\RequestMapping;
 use Psr\Http\Message\ResponseInterface;
 use function Sodium\version_string;
-
+use App\Tools\Result;
 
 class MinioController extends AbstractController
 {
@@ -26,12 +26,10 @@ class MinioController extends AbstractController
      */
     public function listBuckets()
     {
-        var_dump("====");
-        $a = new MinioService();
+        $result = new MinioService();
         // 调用服务层的方法获取存储桶列表
-        $bucketsResponse =$a->listBuckets();
-        var_dump("试试:",$bucketsResponse);
+        $bucketsResponse =$result->listBuckets();
         // 直接返回服务层生成的响应
-        return $bucketsResponse;
+       return Result::success($bucketsResponse);
     }
 }

+ 31 - 32
app/Service/MinioService.php

@@ -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("报错了");
         }
     }
 }