如何使用REST API下载MLFlow中的artifact?
MLflow REST 接口下载 artifact 方法
MLflow 官方提供了对应的 REST 接口,调用逻辑和你用到的 Python API 规则完全对齐,操作步骤如下:
- 先确认你的 MLflow 服务的基础访问地址,比如本地部署的默认地址为
http://localhost:5000 - 构造请求 URL,接口路径固定为
/api/2.0/mlflow/artifacts/get,需要传入两个必填查询参数:run_id:对应你要拉取 artifact 的 run ID,和 Python API 的run_id参数一致path:要下载的 artifact 相对路径,和 Python API 的path参数一致,填空字符串代表拉取对应 run 的 artifact 根目录
- 可选参数:如果要下载整个目录的全部 artifact,可以追加
format=zip参数,接口会直接返回打包好的 zip 压缩包 - 直接发送 GET 请求即可,响应体就是对应 artifact 的文件内容
调用示例
curl 示例
# 下载单个文件,保存到本地为 model.pkl curl -X GET "http://localhost:5000/api/2.0/mlflow/artifacts/get?run_id=你的run_id值&path=model/model.pkl" -o model.pkl # 下载对应 run 的全部 artifact,打包为 zip 保存到本地 curl -X GET "http://localhost:5000/api/2.0/mlflow/artifacts/get?run_id=你的run_id值&path=&format=zip" -o all_artifacts.zip
Python requests 调用示例
import requests MLFLOW_BASE_URL = "http://localhost:5000" run_id = "你的run_id值" artifact_path = "model/model.pkl" resp = requests.get( f"{MLFLOW_BASE_URL}/api/2.0/mlflow/artifacts/get", params={"run_id": run_id, "path": artifact_path} ) # 写入本地文件 with open("model.pkl", "wb") as f: f.write(resp.content)
注意:如果你的 MLflow 服务开启了身份认证,请求时需要在 Headers 中带上对应的认证凭证(比如 Bearer Token),认证逻辑和其他 MLflow REST 接口完全一致。
内容的提问来源于stack exchange,提问作者Nestor
相关产品推荐
相关产品推荐

