config_reader.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. 根据bucket的名字返回对应的s3 AK, SK,endpoint三元组
  3. """
  4. import json
  5. import os
  6. from loguru import logger
  7. def get_s3_config(bucket_name: str):
  8. """
  9. ~/magic_pdf_config.json 读出来
  10. """
  11. if os.name == "posix": # Linux or macOS
  12. home_dir = os.path.expanduser("~")
  13. elif os.name == "nt": # Windows
  14. home_dir = os.path.expandvars("%USERPROFILE%")
  15. else:
  16. raise Exception("Unsupported operating system")
  17. config_file = os.path.join(home_dir, "magic_pdf_config.json")
  18. if not os.path.exists(config_file):
  19. raise Exception("magic_pdf_config.json not found")
  20. with open(config_file, "r") as f:
  21. config = json.load(f)
  22. if bucket_name not in config:
  23. raise Exception("bucket_name not found in magic_pdf_config.json")
  24. ak = config[bucket_name].get("ak")
  25. sk = config[bucket_name].get("sk")
  26. endpoint = config[bucket_name].get("endpoint")
  27. if ak is None or sk is None or endpoint is None:
  28. raise Exception("ak, sk or endpoint not found in magic_pdf_config.json")
  29. # logger.info(f"get_s3_config: ak={ak}, sk={sk}, endpoint={endpoint}")
  30. return ak, sk, endpoint
  31. if __name__ == '__main__':
  32. ak, sk, endpoint = get_s3_config("llm-raw")