

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

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

 使用 AWS Command Line Interface (AWS CLI)，按照以下步骤来开始使用 CodeArtifact。有关更多信息，请参阅 [安装或升级，然后配置 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** 命令对 `my-repo` 存储库配置 npm 程序包管理器。

   ```
   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
   ```

    有关更多信息，请参阅 [配置并使用 npm CodeArtifact](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-domain` 中包含的存储库 `my-repo`。这个存储库有一个 npm 程序包。
   +  `my-domain` 中包含的存储库 `npm-store`。这个存储库与公有 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
          }
      }
      ```