Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

s3_conn.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 boto3
  18. from botocore.exceptions import ClientError
  19. import time
  20. from io import BytesIO
  21. from rag.utils import singleton
  22. from rag import settings
  23. @singleton
  24. class RAGFlowS3(object):
  25. def __init__(self):
  26. self.conn = None
  27. self.s3_config = settings.S3
  28. self.access_key = self.s3_config.get('access_key', None)
  29. self.secret_key = self.s3_config.get('secret_key', None)
  30. self.region = self.s3_config.get('region', None)
  31. self.__open__()
  32. def __open__(self):
  33. try:
  34. if self.conn:
  35. self.__close__()
  36. except Exception:
  37. pass
  38. try:
  39. self.conn = boto3.client(
  40. 's3',
  41. region_name=self.region,
  42. aws_access_key_id=self.access_key,
  43. aws_secret_access_key=self.secret_key
  44. )
  45. except Exception:
  46. logging.exception(f"Fail to connect at region {self.region}")
  47. def __close__(self):
  48. del self.conn
  49. self.conn = None
  50. def bucket_exists(self, bucket):
  51. try:
  52. logging.debug(f"head_bucket bucketname {bucket}")
  53. self.conn.head_bucket(Bucket=bucket)
  54. exists = True
  55. except ClientError:
  56. logging.exception(f"head_bucket error {bucket}")
  57. exists = False
  58. return exists
  59. def health(self):
  60. bucket, fnm, binary = "txtxtxtxt1", "txtxtxtxt1", b"_t@@@1"
  61. if not self.bucket_exists(bucket):
  62. self.conn.create_bucket(Bucket=bucket)
  63. logging.debug(f"create bucket {bucket} ********")
  64. r = self.conn.upload_fileobj(BytesIO(binary), bucket, fnm)
  65. return r
  66. def get_properties(self, bucket, key):
  67. return {}
  68. def list(self, bucket, dir, recursive=True):
  69. return []
  70. def put(self, bucket, fnm, binary):
  71. logging.debug(f"bucket name {bucket}; filename :{fnm}:")
  72. for _ in range(1):
  73. try:
  74. if not self.bucket_exists(bucket):
  75. self.conn.create_bucket(Bucket=bucket)
  76. logging.info(f"create bucket {bucket} ********")
  77. r = self.conn.upload_fileobj(BytesIO(binary), bucket, fnm)
  78. return r
  79. except Exception:
  80. logging.exception(f"Fail put {bucket}/{fnm}")
  81. self.__open__()
  82. time.sleep(1)
  83. def rm(self, bucket, fnm):
  84. try:
  85. self.conn.delete_object(Bucket=bucket, Key=fnm)
  86. except Exception:
  87. logging.exception(f"Fail rm {bucket}/{fnm}")
  88. def get(self, bucket, fnm):
  89. for _ in range(1):
  90. try:
  91. r = self.conn.get_object(Bucket=bucket, Key=fnm)
  92. object_data = r['Body'].read()
  93. return object_data
  94. except Exception:
  95. logging.exception(f"fail get {bucket}/{fnm}")
  96. self.__open__()
  97. time.sleep(1)
  98. return
  99. def obj_exist(self, bucket, fnm):
  100. try:
  101. if self.conn.head_object(Bucket=bucket, Key=fnm):
  102. return True
  103. except ClientError as e:
  104. if e.response['Error']['Code'] == '404':
  105. return False
  106. else:
  107. raise
  108. def get_presigned_url(self, bucket, fnm, expires):
  109. for _ in range(10):
  110. try:
  111. r = self.conn.generate_presigned_url('get_object',
  112. Params={'Bucket': bucket,
  113. 'Key': fnm},
  114. ExpiresIn=expires)
  115. return r
  116. except Exception:
  117. logging.exception(f"fail get url {bucket}/{fnm}")
  118. self.__open__()
  119. time.sleep(1)
  120. return