Browse Source

fastapi+minio:v2
这是更新了配置文件版本

clxHardstudy 1 year ago
parent
commit
6c655d04a7
2 changed files with 47 additions and 0 deletions
  1. 16 0
      config/config.json
  2. 31 0
      config/config.py

+ 16 - 0
config/config.json

@@ -0,0 +1,16 @@
+{
+    "minio": {
+        "endpoint": "127.0.0.1:9000",
+        "access_key": "minioadmin",
+        "secret_key": "minioadmin",
+        "secure": false
+    },
+    "buckets": [
+        "file1",
+        "file2",
+        "file3",
+        "file4",
+        "file5"
+    ]
+}
+

+ 31 - 0
config/config.py

@@ -0,0 +1,31 @@
+"""
+    encoding: UTF-8
+    @author:clx
+    @file:config.py.py
+    @time:2023/5/31 12:48
+"""
+import json
+from minio import Minio
+
+
+# 从配置文件读取设置
+class MinioOperate:
+
+    def __init__(self):
+        with open(r"config/config.json", "r") as f:
+            self.__config = json.load(f)
+        self.minio_client = None
+
+    def link_minio(self):
+        self.minio_client = Minio(**self.__config["minio"])
+        return self.minio_client
+
+    def create_bucket(self):
+        for bucket_name in self.__config['buckets']:
+            if not self.minio_client.bucket_exists(bucket_name):
+                try:
+                    self.minio_client.make_bucket(bucket_name)
+                except Exception as e:
+                    print(f"Bucket creation failed: {e}")
+            else:
+                print(f"Bucket {bucket_name} already exists")