您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

minio_conn.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import time
  2. from minio import Minio
  3. from io import BytesIO
  4. from rag import settings
  5. from rag.utils import singleton
  6. from api.utils.log_utils import logger
  7. @singleton
  8. class RAGFlowMinio(object):
  9. def __init__(self):
  10. self.conn = None
  11. self.__open__()
  12. def __open__(self):
  13. try:
  14. if self.conn:
  15. self.__close__()
  16. except Exception:
  17. pass
  18. try:
  19. self.conn = Minio(settings.MINIO["host"],
  20. access_key=settings.MINIO["user"],
  21. secret_key=settings.MINIO["password"],
  22. secure=False
  23. )
  24. except Exception:
  25. logger.exception(
  26. "Fail to connect %s " % settings.MINIO["host"])
  27. def __close__(self):
  28. del self.conn
  29. self.conn = None
  30. def health(self):
  31. bucket, fnm, binary = "txtxtxtxt1", "txtxtxtxt1", b"_t@@@1"
  32. if not self.conn.bucket_exists(bucket):
  33. self.conn.make_bucket(bucket)
  34. r = self.conn.put_object(bucket, fnm,
  35. BytesIO(binary),
  36. len(binary)
  37. )
  38. return r
  39. def put(self, bucket, fnm, binary):
  40. for _ in range(3):
  41. try:
  42. if not self.conn.bucket_exists(bucket):
  43. self.conn.make_bucket(bucket)
  44. r = self.conn.put_object(bucket, fnm,
  45. BytesIO(binary),
  46. len(binary)
  47. )
  48. return r
  49. except Exception:
  50. logger.exception(f"Fail put {bucket}/{fnm}:")
  51. self.__open__()
  52. time.sleep(1)
  53. def rm(self, bucket, fnm):
  54. try:
  55. self.conn.remove_object(bucket, fnm)
  56. except Exception:
  57. logger.exception(f"Fail put {bucket}/{fnm}:")
  58. def get(self, bucket, fnm):
  59. for _ in range(1):
  60. try:
  61. r = self.conn.get_object(bucket, fnm)
  62. return r.read()
  63. except Exception:
  64. logger.exception(f"Fail put {bucket}/{fnm}:")
  65. self.__open__()
  66. time.sleep(1)
  67. return
  68. def obj_exist(self, bucket, fnm):
  69. try:
  70. if self.conn.stat_object(bucket, fnm):return True
  71. return False
  72. except Exception:
  73. logger.exception(f"Fail put {bucket}/{fnm}:")
  74. return False
  75. def get_presigned_url(self, bucket, fnm, expires):
  76. for _ in range(10):
  77. try:
  78. return self.conn.get_presigned_url("GET", bucket, fnm, expires)
  79. except Exception:
  80. logger.exception(f"Fail put {bucket}/{fnm}:")
  81. self.__open__()
  82. time.sleep(1)
  83. return
  84. MINIO = RAGFlowMinio()
  85. if __name__ == "__main__":
  86. conn = RAGFlowMinio()
  87. fnm = "/opt/home/kevinhu/docgpt/upload/13/11-408.jpg"
  88. from PIL import Image
  89. img = Image.open(fnm)
  90. buff = BytesIO()
  91. img.save(buff, format='JPEG')
  92. print(conn.put("test", "11-408.jpg", buff.getvalue()))
  93. bts = conn.get("test", "11-408.jpg")
  94. img = Image.open(BytesIO(bts))
  95. img.save("test.jpg")