config_reader.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. 根据bucket的名字返回对应的s3 AK, SK,endpoint三元组
  3. """
  4. import json
  5. import os
  6. from loguru import logger
  7. from magic_pdf.libs.commons import parse_bucket_key
  8. def read_config():
  9. home_dir = os.path.expanduser("~")
  10. config_file = os.path.join(home_dir, "magic-pdf.json")
  11. if not os.path.exists(config_file):
  12. raise Exception("magic-pdf.json not found")
  13. with open(config_file, "r") as f:
  14. config = json.load(f)
  15. return config
  16. def get_s3_config(bucket_name: str):
  17. """
  18. ~/magic-pdf.json 读出来
  19. """
  20. config = read_config()
  21. bucket_info = config.get("bucket_info")
  22. if bucket_name not in bucket_info:
  23. access_key, secret_key, storage_endpoint = bucket_info["[default]"]
  24. else:
  25. access_key, secret_key, storage_endpoint = bucket_info[bucket_name]
  26. if access_key is None or secret_key is None or storage_endpoint is None:
  27. raise Exception("ak, sk or endpoint not found in magic-pdf.json")
  28. # logger.info(f"get_s3_config: ak={access_key}, sk={secret_key}, endpoint={storage_endpoint}")
  29. return access_key, secret_key, storage_endpoint
  30. def get_s3_config_dict(path: str):
  31. access_key, secret_key, storage_endpoint = get_s3_config(get_bucket_name(path))
  32. return {"ak": access_key, "sk": secret_key, "endpoint": storage_endpoint}
  33. def get_bucket_name(path):
  34. bucket, key = parse_bucket_key(path)
  35. return bucket
  36. def get_local_dir():
  37. config = read_config()
  38. return config.get("temp-output-dir", "/tmp")
  39. if __name__ == "__main__":
  40. ak, sk, endpoint = get_s3_config("llm-raw")