

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

# 搭配 nuget 或 dotnet CLI 使用 CodeArtifact
<a name="nuget-cli"></a>

您可以使用 `nuget`和 等 CLI 工具`dotnet`，從 CodeArtifact 發佈和使用套件。本文件提供有關設定 CLI 工具以及使用這些工具來發佈或取用套件的資訊。

**Topics**
+ [設定 nuget 或 dotnet CLI](#nuget-configure-cli)
+ [從 CodeArtifact 取用 NuGet 套件](#nuget-consume-cli)
+ [將 NuGet 套件發佈至 CodeArtifact](#nuget-publish-cli)
+ [CodeArtifact NuGet 登入資料提供者參考](#nuget-cred-provider-reference)
+ [CodeArtifact NuGet 登入資料提供者版本](#nuget-cred-provider-history)

## 設定 nuget 或 dotnet CLI
<a name="nuget-configure-cli"></a>

您可以使用 CodeArtifact NuGet 登入資料提供者、 或手動設定 nuget AWS CLI或 dotnet CLI。強烈建議使用登入資料提供者設定 NuGet，以簡化設定和持續身分驗證。

### 方法 1：使用 CodeArtifact NuGet 登入資料提供者設定
<a name="nuget-configure-cli-cred-provider"></a>

CodeArtifact NuGet 登入資料提供者使用 NuGet CLI 工具簡化 CodeArtifact 的身分驗證和組態。CodeArtifact 身分驗證字符的有效期最長為 12 小時。為了避免在使用 nuget 或 dotnet CLI 時手動重新整理權杖，登入資料提供者會在目前的權杖過期之前定期擷取新的權杖。

**重要**  
若要使用登入資料提供者，請確定任何現有的 AWS CodeArtifact 登入資料都已從您的`nuget.config`檔案清除，這些登入資料可能已手動新增或執行 `aws codeartifact login`來設定 NuGet。

**安裝和設定 CodeArtifact NuGet 登入資料提供者**

------
#### [ dotnet ]

1. 使用下列`dotnet`命令，[從 NuGet.org 下載最新版本的 AWS.CodeArtifact.NuGet.CredentialProvider 工具。](https://www.nuget.org/packages/AWS.CodeArtifact.NuGet.CredentialProvider)

   ```
   dotnet tool install -g AWS.CodeArtifact.NuGet.CredentialProvider
   ```

1. 使用 `codeartifact-creds install`命令將登入資料提供者複製到 NuGet 外掛程式資料夾。

   ```
   dotnet codeartifact-creds install
   ```

1. （選用）：設定您要搭配登入資料提供者使用的 AWS 設定檔。如果未設定，登入資料提供者將使用預設設定檔。如需 AWS CLI 設定檔的詳細資訊，請參閱[具名設定檔](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)。

   ```
   dotnet codeartifact-creds configure set profile profile_name
   ```

------
#### [ nuget ]

執行下列步驟，使用 NuGet CLI 從 Amazon S3 儲存貯體安裝 CodeArtifact NuGet 登入資料提供者並進行設定。登入資料提供者將使用預設 AWS CLI 設定檔，如需設定檔的詳細資訊，請參閱[具名設定檔](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)。

1. 從 Amazon S3 儲存貯體下載最新版本的 [CodeArtifact NuGet 登入資料提供者 (codeartifact-nuget-credentialprovider.zip)](https://a.co/dbGqKq7)。

   若要檢視和下載舊版，請參閱 [CodeArtifact NuGet 登入資料提供者版本](#nuget-cred-provider-history)。

1. 解壓縮檔案。

1. 將 **AWS.CodeArtifact.NuGetCredentialProvider** 資料夾從 **netfx** 資料夾複製到 Windows `%user_profile%/.nuget/plugins/netfx/` 或 Linux 或 MacOS `~/.nuget/plugins/netfx` 上的 。

1. 在 Windows `%user_profile%/.nuget/plugins/netcore/`或 `~/.nuget/plugins/netcore` Linux 或 MacOS 上**，將 AWS.CodeArtifact.NuGetCredentialProvider** 資料夾從 **netcore** 資料夾複製到 。

------

建立儲存庫並設定登入資料提供者之後，您可以使用 `nuget`或 `dotnet` CLI 工具來安裝和發佈套件。如需詳細資訊，請參閱[從 CodeArtifact 取用 NuGet 套件](#nuget-consume-cli)及[將 NuGet 套件發佈至 CodeArtifact](#nuget-publish-cli)。

### 方法 2：使用登入命令設定 nuget 或 dotnet
<a name="nuget-configure-login"></a>

中的 `codeartifact login`命令會將儲存庫端點和授權字符 AWS CLI 新增至 NuGet 組態檔案，讓 nuget 或 dotnet 連線到 CodeArtifact 儲存庫。這將修改位於 的使用者層級 NuGet 組態，`%appdata%\NuGet\NuGet.Config`適用於 Windows 和 `~/.config/NuGet/NuGet.Config` `~/.nuget/NuGet/NuGet.Config` Mac/Linux。如需 NuGet 組態的詳細資訊，請參閱[常見 NuGet 組態](https://docs.microsoft.com/en-us/nuget/consume-packages/configuring-nuget-behavior)。

**使用 `login`命令設定 nuget 或 dotnet**

1. 設定您的 AWS 登入資料以搭配 使用 AWS CLI，如中所述[CodeArtifact 入門](getting-started.md)。

1. 確定 NuGet CLI 工具 (`nuget` 或 `dotnet`) 已正確安裝和設定。如需說明，請參閱 [nuget](https://docs.microsoft.com/en-us/nuget/reference/nuget-exe-cli-reference) 或 [dotnet](https://docs.microsoft.com/en-us/dotnet/core/install/) 文件。

1. 使用 CodeArtifact `login`命令來擷取登入資料，以便與 NuGet 搭配使用。
**注意**  
如果您在擁有的網域中存取儲存庫，則不需要包含 `--domain-owner`。如需詳細資訊，請參閱[跨帳戶網域](domain-overview.md#domain-overview-cross-account)。

------
#### [ dotnet ]

**重要**  
**Linux 和 MacOS 使用者：**由於非 Windows 平台不支援加密，因此您擷取的憑證會以純文字形式儲存在組態檔案中。

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

------
#### [ nuget ]

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

------

登入命令將：
+ 使用您的 AWS 登入資料從 CodeArtifact 擷取授權字符。
+ 使用 NuGet 套件來源的新項目來更新您的使用者層級 NuGet 組態。指向 CodeArtifact 儲存庫端點的來源將稱為 `domain_name/repo_name`。

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

建立儲存庫並設定身分驗證之後`dotnet`，您可以使用 `nuget`、 或 `msbuild` CLI 用戶端來安裝和發佈套件。如需詳細資訊，請參閱[從 CodeArtifact 取用 NuGet 套件](#nuget-consume-cli)及[將 NuGet 套件發佈至 CodeArtifact](#nuget-publish-cli)。

### 方法 3：在沒有登入命令的情況下設定 nuget 或 dotnet
<a name="nuget-configure-without-login"></a>

對於手動組態，您必須將儲存庫端點和授權字符新增至 NuGet 組態檔案，以啟用 nuget 或 dotnet 連線至 CodeArtifact 儲存庫。

**手動設定 nuget 或 dotnet 以連接至 CodeArtifact 儲存庫。**

1. 使用 `get-repository-endpoint` AWS CLI 命令判斷 CodeArtifact 儲存庫端點。

   ```
   aws codeartifact get-repository-endpoint --domain my_domain --domain-owner 111122223333 --repository my_repo --format nuget
   ```

   輸出範例：

   ```
   {
      "repositoryEndpoint": "https://my_domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/nuget/my_repo/"
   }
   ```

1. 使用 `get-authorization-token` AWS CLI 命令，從套件管理員取得連接至儲存庫的授權字符。

   ```
   aws codeartifact get-authorization-token --domain my_domain
   ```

   輸出範例：

   ```
   {
      "authorizationToken": "eyJ2I...viOw",
      "expiration": 1601616533.0
   }
   ```

1. 透過附加`/v3/index.json`至步驟 3 `get-repository-endpoint`中 傳回的 URL 來建立完整的儲存庫端點 URL。

1. 設定 nuget 或 dotnet 以使用步驟 1 的儲存庫端點和步驟 2 的授權字符。
**注意**  
來源 URL 必須以 結尾`/v3/index.json`，nuget 或 dotnet 才能成功連線至 CodeArtifact 儲存庫。

------
#### [ dotnet ]

   **Linux 和 MacOS 使用者：**由於非 Windows 平台不支援加密，您必須將 `--store-password-in-clear-text`旗標新增至下列命令。請注意，這會將您的密碼以純文字形式存放在您的組態檔案中。

   ```
   dotnet nuget add source https://my_domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/nuget/my_repo/v3/index.json --name packageSourceName --password eyJ2I...viOw --username aws
   ```

**注意**  
若要更新現有的來源，請使用 `dotnet nuget update source`命令。

------
#### [ nuget ]

   ```
   nuget sources add -name domain_name/repo_name -Source https://my_domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/nuget/my_repo/v3/index.json -password eyJ2I...viOw -username aws
   ```

------

   輸出範例：

   ```
   Package source with Name: domain_name/repo_name added successfully.
   ```
**注意**  
若要使用雙堆疊端點，請使用 `codeartifact.region.on.aws`端點。

## 從 CodeArtifact 取用 NuGet 套件
<a name="nuget-consume-cli"></a>

[使用 CodeArtifact 設定 NuGet ](https://docs.aws.amazon.com//codeartifact/latest/ug/nuget-cli.html)後，您可以取用儲存在 CodeArtifact 儲存庫或其中一個上游儲存庫中的 NuGet 套件。

若要使用 CodeArtifact 儲存庫或其中一個上游儲存庫中的套件版本，請執行下列命令，將 *packageName* 取代為您想要使用的套件名稱`nuget``dotnet`，並使用 NuGet 組態檔案中 CodeArtifact 儲存庫的來源名稱取代 *packageSourceName*。如果您使用 `login`命令來設定 NuGet 組態，來源名稱為 *domain\$1name/repo\$1name*。

**注意**  
請求套件時，NuGet 用戶端會快取該套件的哪些版本存在。由於此行為，在所需版本可用之前，先前請求的套件安裝可能會失敗。若要避免此故障並成功安裝存在的套件，您可以使用 `nuget locals all --clear`或 ，在安裝之前清除 NuGet 快取`dotnet nuget locals all --clear`，或透過提供 的 `-NoCache`選項`nuget`或 的 `--no-cache`選項，避免在 `install`和 `restore`命令期間使用快取`dotnet`。

------
#### [ dotnet ]

```
dotnet add package packageName --source packageSourceName
```

------
#### [ nuget ]

```
nuget install packageName -Source packageSourceName
```

------

**安裝特定版本的套件**

------
#### [ dotnet ]

```
dotnet add package packageName --version 1.0.0 --source packageSourceName
```

------
#### [ nuget ]

```
nuget install packageName -Version 1.0.0 -Source packageSourceName
```

------

如需詳細資訊，請參閱 ** [Microsoft 文件中的使用 nuget.exe CLI 管理套件](https://docs.microsoft.com/en-us/nuget/consume-packages/install-use-packages-nuget-cli)或使用 [dotnet CLI 安裝和管理套件](https://docs.microsoft.com/en-us/nuget/consume-packages/install-use-packages-dotnet-cli)。

### 從 NuGet.org 取用 NuGet 套件
<a name="nuget-consume-nuget-gallery"></a>

您可以透過 CodeArtifact NuGet 儲存庫從 [NuGet.org](https://www.nuget.org/) 使用 NuGet 套件，方法是使用 **NuGet.org** 的外部連線來設定儲存庫。從 **NuGet.org** 取用的套件會擷取並儲存在 CodeArtifact 儲存庫中。如需新增外部連線的詳細資訊，請參閱 [將 CodeArtifact 儲存庫連接至公有儲存庫](external-connection.md)。

## 將 NuGet 套件發佈至 CodeArtifact
<a name="nuget-publish-cli"></a>

[使用 CodeArtifact 設定 NuGet ](https://docs.aws.amazon.com//codeartifact/latest/ug/nuget-cli.html)後，您可以使用 `nuget`或 `dotnet`將套件版本發佈至 CodeArtifact 儲存庫。

若要將套件版本推送至 CodeArtifact 儲存庫，請在 NuGet 組態`.nupkg`檔案中，使用檔案的完整路徑和 CodeArtifact 儲存庫的來源名稱執行下列命令。如果您使用 `login`命令來設定 NuGet 組態，則來源名稱為 `domain_name/repo_name`。

**注意**  
如果您沒有要發佈的 NuGet 套件，您可以建立該套件。如需詳細資訊，請參閱 *Microsoft 文件*中的[套件建立工作流程](https://docs.microsoft.com/en-us/nuget/create-packages/overview-and-workflow)。

------
#### [ dotnet ]

```
dotnet nuget push path/to/nupkg/SamplePackage.1.0.0.nupkg --source packageSourceName
```

------
#### [ nuget ]

```
nuget push path/to/nupkg/SamplePackage.1.0.0.nupkg -Source packageSourceName
```

------

## CodeArtifact NuGet 登入資料提供者參考
<a name="nuget-cred-provider-reference"></a>

CodeArtifact NuGet 登入資料提供者可讓您輕鬆地使用 CodeArtifact 儲存庫設定和驗證 NuGet。

### CodeArtifact NuGet 登入資料提供者命令
<a name="nuget-cred-provider-reference-commands"></a>

本節包含 CodeArtifact NuGet 登入資料提供者的命令清單。這些命令必須以 做為字首，`dotnet codeartifact-creds`如下列範例所示。

```
dotnet codeartifact-creds command
```
+ `configure set profile profile`：設定登入資料提供者以使用提供的 AWS 設定檔。
+ `configure unset profile`：如果已設定，則移除已設定的設定檔。
+ `install`：將登入資料提供者複製到 `plugins` 資料夾。
+ `install --profile profile`：將登入資料提供者複製到 `plugins` 資料夾，並將其設定為使用提供的 AWS 設定檔。
+ `uninstall`：解除安裝登入資料提供者。這不會移除組態檔案的變更。
+ `uninstall --delete-configuration`：解除安裝登入資料提供者，並移除組態檔案的所有變更。

### CodeArtifact NuGet 登入資料提供者日誌
<a name="nuget-cred-provider-reference-logs"></a>

若要啟用 CodeArtifact NuGet 登入資料提供者的記錄，您必須在環境中設定日誌檔案。登入資料提供者日誌包含有用的偵錯資訊，例如：
+ 用於進行連線的 AWS 設定檔
+ 任何身分驗證錯誤
+ 如果提供的端點不是 CodeArtifact URL

**設定 CodeArtifact NuGet 登入資料提供者日誌檔案**

```
export AWS_CODEARTIFACT_NUGET_LOGFILE=/path/to/file
```

設定日誌檔案後，任何`codeartifact-creds`命令都會將其日誌輸出附加至該檔案的內容。

## CodeArtifact NuGet 登入資料提供者版本
<a name="nuget-cred-provider-history"></a>

下表包含 CodeArtifact NuGet 登入資料提供者的版本歷史記錄資訊和下載連結。


| 版本 | 變更 | 發佈日期 | 下載連結 (S3) | 
| --- | --- | --- | --- | 
|  1.0.2 （最新）  |  升級的相依性  |  06/26/2024  |  [下載 v1.0.2](https://d12ov9682v6hj.cloudfront.net/codeartifact-nuget-credentialprovider-v1.0.2.zip)  | 
|  1.0.1  |  新增對 net5、net6 和 SSO 設定檔的支援  |  03/05/2022  |  [下載 v1.0.1](https://a.co/cAIkhV1)  | 
|  1.0.0  |  初始 CodeArtifact NuGet 登入資料提供者版本  |  11/20/2020  |  [下載 v1.0.0](https://a.co/8b2cENb)  | 