

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 CodeArtifact 設定和使用雙身
<a name="python-configure-twine"></a>

[Twine](https://pypi.org/project/twine/) 是 Python 套件的套件發佈公用程式。若要使用孿生將 Python 套件發佈到您的 CodeArtifact 儲存庫，您必須先使用 CodeArtifact 儲存庫資訊和登入資料來設定孿生。

Twine 只能用於發佈 Python 套件。若要安裝 Python 套件，您可以使用 [pip](https://pypi.org/project/pip/)。如需詳細資訊，請參閱[搭配 CodeArtifact 設定和使用 pip](python-configure-pip.md)。

## 使用 `login`命令設定雙身
<a name="python-configure-twine-login"></a>

首先，設定您的 AWS 登入資料以與 搭配使用 AWS CLI，如中所述[CodeArtifact 入門](getting-started.md)。然後，使用 CodeArtifact `login`命令來擷取憑證，並與他們設定雙身。

**注意**  
如果您在擁有的網域中存取儲存庫，則不需要包含 `--domain-owner`。如需詳細資訊，請參閱[跨帳戶網域](domain-overview.md#domain-overview-cross-account)。

若要設定雙工，請執行下列命令。

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

 `login` 使用您的 AWS 登入資料從 CodeArtifact 擷取授權字符。`login` 命令會編輯 `~/.pypirc`以使用 登入資料新增 `--repository`選項指定的儲存庫，藉此設定雙工以搭配 CodeArtifact 使用。

呼叫 後的預設授權期間`login`為 12 小時，`login`必須呼叫 以定期重新整理字符。如需使用 `login`命令建立的授權字符的詳細資訊，請參閱 [使用 `login`命令建立的字符](tokens-authentication.md#auth-token-login)。

## 不使用 `login`命令設定雙身
<a name="python-configure-twine-without-login"></a>

 如果您無法使用 `login`命令來設定雙身，您可以使用 `~/.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`
```

## 執行雙身
<a name="python-run-twine"></a>

使用分身發佈 Python 套件資產之前，您必須先設定 CodeArtifact 許可和資源。

1. 請依照 [使用 AWS CodeArtifact 設定](get-set-up-for-codeartifact.md)區段中的步驟來設定 AWS 您的帳戶、工具和許可。

1. 遵循 [使用 `login`命令設定雙身](#python-configure-twine-login)或 中的步驟來設定分身[不使用 `login`命令設定雙身](#python-configure-twine-without-login)。

設定雙工之後，您可以執行`twine`命令。使用下列命令來發佈 Python 套件資產。

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

如需有關如何建置和封裝 Python 應用程式的資訊，請參閱 Python Packaging Authority [網站上的產生分佈封存](https://packaging.python.org/tutorials/packaging-projects/#generating-distribution-archives)。