選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

azure_spn_conn.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import logging
  2. import os
  3. import time
  4. from rag import settings
  5. from rag.utils import singleton
  6. from azure.identity import ClientSecretCredential, AzureAuthorityHosts
  7. from azure.storage.filedatalake import FileSystemClient
  8. @singleton
  9. class RAGFlowAzureSpnBlob(object):
  10. def __init__(self):
  11. self.conn = None
  12. self.account_url = os.getenv('ACCOUNT_URL', settings.AZURE["account_url"])
  13. self.client_id = os.getenv('CLIENT_ID', settings.AZURE["client_id"])
  14. self.secret = os.getenv('SECRET', settings.AZURE["secret"])
  15. self.tenant_id = os.getenv('TENANT_ID', settings.AZURE["tenant_id"])
  16. self.container_name = os.getenv('CONTAINER_NAME', settings.AZURE["container_name"])
  17. self.__open__()
  18. def __open__(self):
  19. try:
  20. if self.conn:
  21. self.__close__()
  22. except Exception:
  23. pass
  24. try:
  25. credentials = ClientSecretCredential(tenant_id=self.tenant_id, client_id=self.client_id, client_secret=self.secret, authority=AzureAuthorityHosts.AZURE_CHINA)
  26. self.conn = FileSystemClient(account_url=self.account_url, file_system_name=self.container_name, credential=credentials)
  27. except Exception:
  28. logging.exception("Fail to connect %s" % self.account_url)
  29. def __close__(self):
  30. del self.conn
  31. self.conn = None
  32. def health(self):
  33. _bucket, fnm, binary = "txtxtxtxt1", "txtxtxtxt1", b"_t@@@1"
  34. f = self.conn.create_file(fnm)
  35. f.append_data(binary, offset=0, length=len(binary))
  36. return f.flush_data(len(binary))
  37. def put(self, bucket, fnm, binary):
  38. for _ in range(3):
  39. try:
  40. f = self.conn.create_file(fnm)
  41. f.append_data(binary, offset=0, length=len(binary))
  42. return f.flush_data(len(binary))
  43. except Exception:
  44. logging.exception(f"Fail put {bucket}/{fnm}")
  45. self.__open__()
  46. time.sleep(1)
  47. def rm(self, bucket, fnm):
  48. try:
  49. self.conn.delete_file(fnm)
  50. except Exception:
  51. logging.exception(f"Fail rm {bucket}/{fnm}")
  52. def get(self, bucket, fnm):
  53. for _ in range(1):
  54. try:
  55. client = self.conn.get_file_client(fnm)
  56. r = client.download_file()
  57. return r.read()
  58. except Exception:
  59. logging.exception(f"fail get {bucket}/{fnm}")
  60. self.__open__()
  61. time.sleep(1)
  62. return
  63. def obj_exist(self, bucket, fnm):
  64. try:
  65. client = self.conn.get_file_client(fnm)
  66. return client.exists()
  67. except Exception:
  68. logging.exception(f"Fail put {bucket}/{fnm}")
  69. return False
  70. def get_presigned_url(self, bucket, fnm, expires):
  71. for _ in range(10):
  72. try:
  73. return self.conn.get_presigned_url("GET", bucket, fnm, expires)
  74. except Exception:
  75. logging.exception(f"fail get {bucket}/{fnm}")
  76. self.__open__()
  77. time.sleep(1)
  78. return