跳转至

上传文件

POST /api/v5/repos/*/*/file/upload

该接口用于将附件文件上传至指定仓库,文件大小限制为20M。上传成功后返回文件的相对路径与完整访问地址,用于后续访问或引用该文件。

访问令牌支持说明

  • AtomGit 用户访问令牌(AtomGit user access tokens)

是否允许匿名访问

此接口不允许匿名访问

请求参数

参数名 位置 类型 必填 说明
owner path string 是 仓库所属空间地址(企业、组织或个人的地址path)
repo path string 是 仓库路径(path)
access_token query string 是 用户授权码
file form-data file 是 上传的附件,文件大小限制为 20M

响应参数

字段 类型 必返 可为空 说明
success boolean 是 否 是否上传成功
path string 是 是 上传成功后的相对路径,可能为 null
full_path string 是 是 上传成功后的完整访问地址,可能为 null

失败响应

只有 200/成功响应是不够的:调用方需要知道失败长什么样才能写错误处理。

400 参数错误

{
  "error": "invalid_request",
  "message": "文件不能为空",
  "field": "file"
}

401 未认证

{
  "error": "unauthorized",
  "message": "缺少或无效的访问令牌"
}

403 禁止访问

{
  "error": "forbidden",
  "message": "没有权限执行该操作"
}

错误字段名以后端实际返回为准。

HTTP 响应状态码

状态码 说明
200 成功(OK)
400 参数错误(Bad request)
401 需要认证(Requires authentication)
403 禁止访问(Forbidden)
500 系统内部错误(Internal server error)

请求示例

参数说明

示例中的占位符含义:<owner> 替换为仓库所属空间地址(企业、组织或个人的地址 path);<repo> 替换为仓库路径(path);<token> 替换为你的 AtomGit 用户访问令牌;<FILE> 替换为本地要上传的文件。

cURL

curl -X POST 'https://api.atomgit.com/api/v5/repos/<owner>/<repo>/file/upload?access_token=<token>' \
  -H 'Accept: application/json' \
  -F 'file=@<FILE>'

JavaScript

// 原生 fetch (Node.js 18+ / 浏览器)
const formData = new FormData();
formData.append("file", fileInput.files[0]);

const response = await fetch(
  "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/file/upload?access_token=<token>",
  {
    method: "POST",
    headers: {
      "Accept": "application/json",
    },
    body: formData,
  }
);

if (!response.ok) {
  throw new Error(`请求失败: ${response.status}`);
}

const data = await response.json();
console.log(data);

Python

import requests

resp = requests.post(
    "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/file/upload?access_token=<token>",
    files={"file": open("<FILE>", "rb")},
    headers={"Accept": "application/json"},
    timeout=10,
)
resp.raise_for_status()

print(resp.json())

响应示例

状态码: 200

{
  "success": true,
  "path": "uploads/4c98ad75-729f-49ce-82ab-b41c1f7ffc90/test3.txt",
  "full_path": "https://api.atomgit.com/xiaogang/test/attachment/uploads/4c98ad75-729f-49ce-82ab-b41c1f7ffc90/test3.txt"
}

错误码

错误码 说明
400 参数错误或请求无法解析
401 未认证或凭证无效(缺少/无效的访问凭证)
403 没有权限执行该操作
404 请求路径不存在
405 请求方法不支持
429 请求过于频繁,触发限流
500 系统内部错误

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 gitcode-dev/atomgit-api-doc