在 CodeArtifact 中配置和使用 twine - CodeArtifact

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在 CodeArtifact 中配置和使用 twine

twine 是一款适用于 Python 程序包的程序包发布实用程序。要使用 twine 将 Python 程序包发布到 CodeArtifact 存储库,您必须先使用 CodeArtifact 存储库信息和凭证来配置 twine。

twine 只能用于发布 Python 程序包。要安装 Python 程序包,您可以使用 pip。有关更多信息,请参阅在 CodeArtifact 中配置和使用 pip

使用 login 命令配置 twine

首先,配置要与 AWS CLI 一起使用的 AWS 凭证,如 开始使用 CodeArtifact中所述。然后,使用 CodeArtifact login 命令来提取凭证并使用这些凭证来配置 twine。

注意

如果您要访问您拥有的域中的存储库,则无需包括 --domain-owner。有关更多信息,请参阅跨账户域

要配置 twine,请运行以下命令。

aws codeartifact login --tool twine --domain my_domain --domain-owner 111122223333 --repository my_repo

login 使用您的 AWS 凭证从 CodeArtifact 提取授权令牌。login 命令会编辑 ~/.pypirc,添加 --repository 选项指定的存储库和凭证,从而将 twine 配置为与 CodeArtifact 一起使用。

调用 login 后的默认授权期为 12 小时,且必须调用 login 来定期刷新令牌。有关使用 login 命令创建的授权令牌的更多信息,请参阅使用 login 命令创建的令牌

不使用 login 命令配置 twine

如果无法使用 login 命令来配置 twine,则可以使用 ~/.pypirc 文件或环境变量。要使用 ~/.pypirc 文件,请在文件添加以下条目。密码必须是 get-authorization-token API 获取的身份验证令牌。

[distutils] index-servers = codeartifact [codeartifact] repository = https://my_domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/pypi/my_repo/ password = auth-token username = aws

要使用环境变量,请执行以下操作:

注意

如果您要访问您拥有的域中的存储库,则无需包括 --domain-owner。有关更多信息,请参阅跨账户域

export TWINE_USERNAME=aws export TWINE_PASSWORD=`aws codeartifact get-authorization-token --domain my_domain --domain-owner 111122223333 --query authorizationToken --output text` export TWINE_REPOSITORY_URL=`aws codeartifact get-repository-endpoint --domain my_domain --domain-owner 111122223333 --repository my_repo --format pypi --query repositoryEndpoint --output text`

运行 twine

在使用 twine 来发布 Python 程序包资产之前,必须先配置 CodeArtifact 权限和资源。

  1. 按照 设置 AWS CodeArtifact 部分中的步骤来配置您的 AWS 账户、工具和权限。

  2. 按照 使用 login 命令配置 twine不使用 login 命令配置 twine 中的步骤配置 twine。

配置 twine 之后,就可以运行 twine 命令。使用以下命令来发布 Python 程序包资产。

twine upload --repository codeartifact mypackage-1.0.tgz

有关如何构建和打包 Python 应用程序的信息,请参阅 Python 打包权威机构网站上的生成分发存档