config_reader.py 1.3 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. access_key = config[bucket_name].get("ak")
  25. secret_key = config[bucket_name].get("sk")
  26. storage_endpoint = config[bucket_name].get("endpoint")
  27. if access_key is None or secret_key is None or storage_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={access_key}, sk={secret_key}, endpoint={storage_endpoint}")
  30. return access_key, secret_key, storage_endpoint
  31. if __name__ == '__main__':
  32. ak, sk, endpoint = get_s3_config("llm-raw")