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

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