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.

azure_sas_conn.py 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 os
  18. import time
  19. from io import BytesIO
  20. from rag import settings
  21. from rag.utils import singleton
  22. from azure.storage.blob import ContainerClient
  23. @singleton
  24. class RAGFlowAzureSasBlob(object):
  25. def __init__(self):
  26. self.conn = None
  27. self.container_url = os.getenv('CONTAINER_URL', settings.AZURE["container_url"])
  28. self.sas_token = os.getenv('SAS_TOKEN', settings.AZURE["sas_token"])
  29. self.__open__()
  30. def __open__(self):
  31. try:
  32. if self.conn:
  33. self.__close__()
  34. except Exception:
  35. pass
  36. try:
  37. self.conn = ContainerClient.from_container_url(self.container_url + "?" + self.sas_token)
  38. except Exception:
  39. logging.exception("Fail to connect %s " % self.container_url)
  40. def __close__(self):
  41. del self.conn
  42. self.conn = None
  43. def health(self):
  44. _bucket, fnm, binary = "txtxtxtxt1", "txtxtxtxt1", b"_t@@@1"
  45. return self.conn.upload_blob(name=fnm, data=BytesIO(binary), length=len(binary))
  46. def put(self, bucket, fnm, binary):
  47. for _ in range(3):
  48. try:
  49. return self.conn.upload_blob(name=fnm, data=BytesIO(binary), length=len(binary))
  50. except Exception:
  51. logging.exception(f"Fail put {bucket}/{fnm}")
  52. self.__open__()
  53. time.sleep(1)
  54. def rm(self, bucket, fnm):
  55. try:
  56. self.conn.delete_blob(fnm)
  57. except Exception:
  58. logging.exception(f"Fail rm {bucket}/{fnm}")
  59. def get(self, bucket, fnm):
  60. for _ in range(1):
  61. try:
  62. r = self.conn.download_blob(fnm)
  63. return r.read()
  64. except Exception:
  65. logging.exception(f"fail get {bucket}/{fnm}")
  66. self.__open__()
  67. time.sleep(1)
  68. return
  69. def obj_exist(self, bucket, fnm):
  70. try:
  71. return self.conn.get_blob_client(fnm).exists()
  72. except Exception:
  73. logging.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. logging.exception(f"fail get {bucket}/{fnm}")
  81. self.__open__()
  82. time.sleep(1)
  83. return