

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

# 開始使用 AWS CLI
<a name="getting-started-cli"></a>

 執行下列步驟，以使用 AWS Command Line Interface () 開始使用 CodeArtifact AWS CLI。如需詳細資訊，請參閱[安裝或升級 ，然後設定 AWS CLI](get-set-up-install-cli.md)。本指南使用 `npm`套件管理工具，如果您使用不同的套件管理工具，則需要修改下列部分步驟。

1.  使用 AWS CLI 執行 **create-domain**命令。

   ```
   aws codeartifact create-domain --domain my-domain
   ```

    JSON 格式的資料會顯示在輸出中，其中包含新網域的詳細資訊。

   ```
   {
       "domain": {
           "name": "my-domain",
           "owner": "111122223333",
           "arn": "arn:aws:codeartifact:us-west-2:111122223333:domain/my-domain",
           "status": "Active",
           "createdTime": "2020-10-07T15:36:35.194000-04:00",
           "encryptionKey": "arn:aws:kms:us-west-2:111122223333:key/your-kms-key",
           "repositoryCount": 0,
           "assetSizeBytes": 0
       }
   }
   ```

   如果您收到錯誤 `Could not connect to the endpoint URL`，請確定您的 AWS CLI 已設定，且您的**預設區域名稱**已設定為您建立儲存庫的相同區域，請參閱[設定 AWS 命令列界面](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)。

1.  使用 **create-repository**命令在您的網域中建立儲存庫。

   ```
   aws codeartifact create-repository --domain my-domain --domain-owner 111122223333 --repository my-repo
   ```

    JSON 格式的資料會顯示在輸出中，其中包含新儲存庫的詳細資訊。

   ```
   {
       "repository": {
           "name": "my-repo",
           "administratorAccount": "111122223333",
           "domainName": "my-domain",
           "domainOwner": "111122223333",
           "arn": "arn:aws:codeartifact:us-west-2:111122223333:repository/my-domain/my-repo",
           "upstreams": [],
           "externalConnections": []
       }
   }
   ```

1. 使用 **create-repository**命令為您的儲存庫建立上游`my-repo`儲存庫。

   ```
   aws codeartifact create-repository --domain my-domain --domain-owner 111122223333 --repository npm-store
   ```

    JSON 格式的資料會顯示在輸出中，其中包含新儲存庫的詳細資訊。

   ```
   {
       "repository": {
           "name": "npm-store",
           "administratorAccount": "111122223333",
           "domainName": "my-domain",
           "domainOwner": "111122223333",
           "arn": "arn:aws:codeartifact:us-west-2:111122223333:repository/my-domain/npm-store",
           "upstreams": [],
           "externalConnections": []
       }
   }
   ```

1.  使用 **associate-external-connection**命令將外部連線新增至 npm 公有儲存庫。 `npm-store`

   ```
   aws codeartifact associate-external-connection --domain my-domain --domain-owner 111122223333 --repository npm-store --external-connection "public:npmjs"
   ```

    JSON 格式的資料會顯示在輸出中，其中包含儲存庫及其新外部連線的詳細資訊。

   ```
   {
       "repository": {
           "name": "npm-store",
           "administratorAccount": "111122223333",
           "domainName": "my-domain",
           "domainOwner": "111122223333",
           "arn": "arn:aws:codeartifact:us-west-2:111122223333:repository/my-domain/npm-store",
           "upstreams": [],
           "externalConnections": [
               {
                   "externalConnectionName": "public:npmjs",
                   "packageFormat": "npm",
                   "status": "AVAILABLE"
               }
           ]
       }
   }
   ```

    如需詳細資訊，請參閱[將 CodeArtifact 儲存庫連接至公有儲存庫](external-connection.md)。

1.  使用 **update-repository**命令將儲存`npm-store`庫做為上游儲存庫與`my-repo`儲存庫建立關聯。

   ```
   aws codeartifact update-repository --repository my-repo --domain my-domain --domain-owner 111122223333 --upstreams repositoryName=npm-store
   ```

    JSON 格式的資料會顯示在輸出中，其中包含更新儲存庫的詳細資訊，包括其新的上游儲存庫。

   ```
   {
       "repository": {
           "name": "my-repo",
           "administratorAccount": "111122223333",
           "domainName": "my-domain",
           "domainOwner": "111122223333",
           "arn": "arn:aws:codeartifact:us-west-2:111122223333:repository/my-domain/my-repo",
           "upstreams": [
               {
                   "repositoryName": "npm-store"
               }
           ],
           "externalConnections": []
       }
   }
   ```

    如需詳細資訊，請參閱[新增或移除上游儲存庫 (AWS CLI)](repo-upstream-add.md#repo-upstream-add-cli)。

1. 使用 **login**命令來設定 npm 套件管理員與`my-repo`儲存庫。

   ```
   aws codeartifact login --tool npm --repository my-repo --domain my-domain --domain-owner 111122223333
   ```

   您應該會收到確認登入成功的輸出。

   ```
   Successfully configured npm to use AWS CodeArtifact repository https://my-domain-111122223333.d.codeartifact.us-east-2.amazonaws.com/npm/my-repo/
   Login expires in 12 hours at 2020-10-08 02:45:33-04:00
   ```

    如需詳細資訊，請參閱[搭配 CodeArtifact 設定和使用 npm](npm-auth.md)。

1.  使用 npm CLI 來安裝 npm 套件。例如，若要安裝熱門的 npm 套件 `lodash`，請使用下列命令。

   ```
   npm install lodash
   ```

1.  使用 **list-packages**命令來檢視您剛安裝在`my-repo`儲存庫中的套件。
**注意**  
安裝`npm install`命令完成與儲存庫中顯示套件之間可能會有延遲。如需從公有儲存庫擷取套件時一般延遲的詳細資訊，請參閱 [外部連線延遲](external-connection-requesting-packages.md#external-connection-latency)。

   ```
   aws codeartifact list-packages --domain my-domain --repository my-repo
   ```

    JSON 格式的資料會顯示在輸出中，其中包含您安裝的套件格式和名稱。

   ```
   {
       "packages": [
           {
               "format": "npm",
               "package": "lodash"
           }
       ]
   }
   ```

   您現在有三個 CodeArtifact 資源：
   +  網域 `my-domain`。
   +  中包含`my-repo`的儲存庫`my-domain`。此儲存庫有可用的 npm 套件。
   +  中包含`npm-store`的儲存庫`my-domain`。此儲存庫與公有 npm 儲存庫具有外部連線，並作為上游儲存庫與`my-repo`儲存庫建立關聯。

1. 若要避免進一步 AWS 收費，請刪除您在本教學課程中使用的資源：
**注意**  
 您無法刪除包含儲存庫的網域，因此您必須先刪除 `my-repo`和 ，`npm-store`再刪除 `my-domain`。

   1.  使用 **delete-repository**命令來刪除儲存`npm-store`庫。

      ```
      aws codeartifact delete-repository --domain my-domain --domain-owner 111122223333 --repository my-repo
      ```

       JSON 格式的資料會顯示在輸出中，其中包含已刪除儲存庫的詳細資訊。

      ```
      {
          "repository": {
              "name": "my-repo",
              "administratorAccount": "111122223333",
              "domainName": "my-domain",
              "domainOwner": "111122223333",
              "arn": "arn:aws:codeartifact:us-west-2:111122223333:repository/my-domain/my-repo",
              "upstreams": [
                  {
                      "repositoryName": "npm-store"
                  }
              ],
              "externalConnections": []
          }
      }
      ```

   1.  使用 **delete-repository**命令來刪除儲存`npm-store`庫。

      ```
      aws codeartifact delete-repository --domain my-domain --domain-owner 111122223333 --repository npm-store
      ```

       JSON 格式的資料會顯示在輸出中，其中包含已刪除儲存庫的詳細資訊。

      ```
      {
          "repository": {
              "name": "npm-store",
              "administratorAccount": "111122223333",
              "domainName": "my-domain",
              "domainOwner": "111122223333",
              "arn": "arn:aws:codeartifact:us-west-2:111122223333:repository/my-domain/npm-store",
              "upstreams": [],
              "externalConnections": [
                  {
                      "externalConnectionName": "public:npmjs",
                      "packageFormat": "npm",
                      "status": "AVAILABLE"
                  }
              ]
          }
      }
      ```

   1. 使用 **delete-domain**命令來刪除儲存`my-domain`庫。

      ```
      aws codeartifact delete-domain --domain my-domain --domain-owner 111122223333
      ```

       JSON 格式的資料會顯示在輸出中，其中包含已刪除網域的詳細資訊。

      ```
      {
          "domain": {
              "name": "my-domain",
              "owner": "111122223333",
              "arn": "arn:aws:codeartifact:us-west-2:111122223333:domain/my-domain",
              "status": "Deleted",
              "createdTime": "2020-10-07T15:36:35.194000-04:00",
              "encryptionKey": "arn:aws:kms:us-west-2:111122223333:key/your-kms-key",
              "repositoryCount": 0,
              "assetSizeBytes": 0
          }
      }
      ```