config_reader.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 get_s3_config(bucket_name: str):
  9. """
  10. ~/magic-pdf.json 读出来
  11. """
  12. home_dir = os.path.expanduser("~")
  13. config_file = os.path.join(home_dir, "magic-pdf.json")
  14. if not os.path.exists(config_file):
  15. raise Exception("magic-pdf.json not found")
  16. with open(config_file, "r") as f:
  17. config = json.load(f)
  18. bucket_info = config.get("bucket_info")
  19. if bucket_name not in bucket_info:
  20. access_key, secret_key, storage_endpoint = bucket_info["[default]"]
  21. else:
  22. access_key, secret_key, storage_endpoint = bucket_info[bucket_name]
  23. if access_key is None or secret_key is None or storage_endpoint is None:
  24. raise Exception("ak, sk or endpoint not found in magic-pdf.json")
  25. # logger.info(f"get_s3_config: ak={access_key}, sk={secret_key}, endpoint={storage_endpoint}")
  26. return access_key, secret_key, storage_endpoint
  27. def get_s3_config_dict(path: str):
  28. access_key, secret_key, storage_endpoint = get_s3_config(get_bucket_name(path))
  29. return {"ak": access_key, "sk": secret_key, "endpoint": storage_endpoint}
  30. def get_bucket_name(path):
  31. bucket, key = parse_bucket_key(path)
  32. return bucket
  33. if __name__ == '__main__':
  34. ak, sk, endpoint = get_s3_config("llm-raw")