Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import logging
  17. import time
  18. from minio import Minio
  19. from minio.error import S3Error
  20. from io import BytesIO
  21. from rag import settings
  22. from rag.utils import singleton
  23. @singleton
  24. class RAGFlowMinio:
  25. def __init__(self):
  26. self.conn = None
  27. self.__open__()
  28. def __open__(self):
  29. try:
  30. if self.conn:
  31. self.__close__()
  32. except Exception:
  33. pass
  34. try:
  35. self.conn = Minio(settings.MINIO["host"],
  36. access_key=settings.MINIO["user"],
  37. secret_key=settings.MINIO["password"],
  38. secure=False
  39. )
  40. except Exception:
  41. logging.exception(
  42. "Fail to connect %s " % settings.MINIO["host"])
  43. def __close__(self):
  44. del self.conn
  45. self.conn = None
  46. def health(self):
  47. bucket, fnm, binary = "txtxtxtxt1", "txtxtxtxt1", b"_t@@@1"
  48. if not self.conn.bucket_exists(bucket):
  49. self.conn.make_bucket(bucket)
  50. r = self.conn.put_object(bucket, fnm,
  51. BytesIO(binary),
  52. len(binary)
  53. )
  54. return r
  55. def put(self, bucket, fnm, binary):
  56. for _ in range(3):
  57. try:
  58. if not self.conn.bucket_exists(bucket):
  59. self.conn.make_bucket(bucket)
  60. r = self.conn.put_object(bucket, fnm,
  61. BytesIO(binary),
  62. len(binary)
  63. )
  64. return r
  65. except Exception:
  66. logging.exception(f"Fail to put {bucket}/{fnm}:")
  67. self.__open__()
  68. time.sleep(1)
  69. def rm(self, bucket, fnm):
  70. try:
  71. self.conn.remove_object(bucket, fnm)
  72. except Exception:
  73. logging.exception(f"Fail to remove {bucket}/{fnm}:")
  74. def get(self, bucket, filename):
  75. for _ in range(1):
  76. try:
  77. r = self.conn.get_object(bucket, filename)
  78. return r.read()
  79. except Exception:
  80. logging.exception(f"Fail to get {bucket}/{filename}")
  81. self.__open__()
  82. time.sleep(1)
  83. return
  84. def obj_exist(self, bucket, filename):
  85. try:
  86. if not self.conn.bucket_exists(bucket):
  87. return False
  88. if self.conn.stat_object(bucket, filename):
  89. return True
  90. else:
  91. return False
  92. except S3Error as e:
  93. if e.code in ["NoSuchKey", "NoSuchBucket", "ResourceNotFound"]:
  94. return False
  95. except Exception:
  96. logging.exception(f"obj_exist {bucket}/{filename} got exception")
  97. return False
  98. def get_presigned_url(self, bucket, fnm, expires):
  99. for _ in range(10):
  100. try:
  101. return self.conn.get_presigned_url("GET", bucket, fnm, expires)
  102. except Exception:
  103. logging.exception(f"Fail to get_presigned {bucket}/{fnm}:")
  104. self.__open__()
  105. time.sleep(1)
  106. return
  107. MINIO = RAGFlowMinio()
  108. if __name__ == "__main__":
  109. conn = RAGFlowMinio()
  110. fnm = "/opt/home/kevinhu/docgpt/upload/13/11-408.jpg"
  111. from PIL import Image
  112. img = Image.open(fnm)
  113. buff = BytesIO()
  114. img.save(buff, format='JPEG')
  115. print(conn.put("test", "11-408.jpg", buff.getvalue()))
  116. bts = conn.get("test", "11-408.jpg")
  117. img = Image.open(BytesIO(bts))
  118. img.save("test.jpg")