进阶 docs.pypi.org 2026-10-20 06:39:13 · 24 阅读

第27章 PyPI Upload API:包上传接口详解

Upload API

位于 https://upload.pypi.org/legacy/ 的 API 端点是 Warehouse 对 PyPI 旧版上传 API 的模拟实现。 twine 等工具通过此端点向 PyPI 上传分发包。

路由 上传文件

重要 PyPI 上的版本(Releases)通过逐个上传文件来创建。 新版本中上传的第一个文件会为该版本创建一个 release,并填充其元数据。

路由:POST upload.pypi.org/legacy/ 上传 API 允许通过发送包含以下字段的多部分表单(multipart/form-data)POST 请求来上传制品:

:action 设置为 file_upload protocol_version 设置为 1 content 为要上传的文件及其正确的文件名 (例如 my_foo_bar-4.2-cp36-cp36m-manylinux1_x86_64.whl) 以下哈希摘要之一: md5_digest 设置为上传文件的 md5 哈希值,采用无填充的 urlsafe base64 编码 sha256_digest 设置为 SHA-2-256 哈希值,采用十六进制编码 blake2_256_digest 设置为 Blake2b 256 位哈希值,采用十六进制编码

filetype 必须设置为制品的类型:bdist_wheel 或 sdist。 pyversion 在 bdist_wheel 上传时必须设置为 Python 标签(tag), 在 sdist 上传时必须设置为 source。 metadata_version、name 和 version 必须根据 核心元数据规范(Core metadata specifications)进行设置 attestations 可设置为证明对象(attestation objects)的 JSON 数组。 如果 PyPI 无法验证提供的每个证明,它将拒绝该上传。

可以根据核心元数据规范设置其他任何字段。 所有字段名需转换为小写,连字符需替换为下划线。此外,多值字段(如 Classifier) 需使用复数形式(如 classifiers),但存在以下有限例外:

元数据字段 表单字段

Platform platform(而非 platforms)

Supported-Platform supported_platform(而非 supported_platforms)

License-File license_file(而非 license_files)

警告 必须执行上述转换。 提交如 Description-Content-Type 等包含连字符的表单字段不会导致报错, 但会被静默忽略。

评论 (0)