

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

# 配置和使用麻线 CodeArtifact
<a name="python-configure-twine"></a>

[twine](https://pypi.org/project/twine/) 是一款适用于 Python 程序包的程序包发布实用程序。要使用 twine 将 Python 包发布到 CodeArtifact 仓库，必须先使用仓 CodeArtifact 库信息和凭据配置 twine。

twine 只能用于发布 Python 程序包。要安装 Python 程序包，您可以使用 [pip](https://pypi.org/project/pip/)。有关更多信息，请参阅 [配置并使用 pip CodeArtifact](python-configure-pip.md)。

## 使用 `login` 命令配置 twine
<a name="python-configure-twine-login"></a>

首先，配置您的 AWS 凭证以与一起使用 AWS CLI，如中所述[开始使用 CodeArtifact](getting-started.md)。然后，使用 CodeArtifact`login`命令获取凭据并使用它们配置 twine。

**注意**  
如果您要访问您拥有的域中的存储库，则无需包括 `--domain-owner`。有关更多信息，请参阅 [跨账户域](domain-overview.md#domain-overview-cross-account)。

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

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

 `login` CodeArtifact 使用您的 AWS 凭证获取授权令牌。该`login`命令将 twine 配置为 CodeArtifact 通过编辑`~/.pypirc`添加`--repository`选项指定的带有凭据的存储库来使用。

调用 `login` 后的默认授权期为 12 小时，且必须调用 `login` 来定期刷新令牌。有关使用 `login` 命令创建的授权令牌的更多信息，请参阅 [使用 `login` 命令创建的令牌](tokens-authentication.md#auth-token-login)。

## 不使用 `login` 命令配置 twine
<a name="python-configure-twine-without-login"></a>

 如果无法使用 `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
```

**注意**  
要使用双堆栈端点，请使用 `codeartifact.region.on.aws` 端点。

要使用环境变量，请执行以下操作：

**注意**  
如果您要访问您拥有的域中的存储库，则无需包括 `--domain-owner`。有关更多信息，请参阅 [跨账户域](domain-overview.md#domain-overview-cross-account)。

```
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
<a name="python-run-twine"></a>

在使用 twine 发布 Python 包资源之前，必须先配置 CodeArtifact 权限和资源。

1. 按照[使用进行设置 AWS CodeArtifact](get-set-up-for-codeartifact.md)部分中的步骤配置您的 AWS 账户、工具和权限。

1. 按照 [使用 `login` 命令配置 twine](#python-configure-twine-login) 或 [不使用 `login` 命令配置 twine](#python-configure-twine-without-login) 中的步骤配置 twine。

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

```
twine upload --repository codeartifact mypackage-1.0.tgz
```

有关如何构建和打包 Python 应用程序的信息，请参阅 Python 打包权威机构网站上的[生成分发存档](https://packaging.python.org/tutorials/packaging-projects/#generating-distribution-archives)。