

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

# 使用的 HTTPS 连接 AWS CodeCommit 的设置步骤 git-remote-codecommit
<a name="setting-up-git-remote-codecommit"></a>

如果您想 CodeCommit 使用根账户、联合访问或临时证书进行连接，则应使用设置访问权限**git-remote-codecommit**。此实用程序提供了一种通过扩展 Git 从 CodeCommit 存储库中推送和提取代码的简单方法。这是支持使用联合访问、身份提供程序和临时凭证建立连接的推荐方法。要向联合身份分配权限，您可以创建角色并为角色定义权限。当联合身份进行身份验证时，该身份将与角色相关联并被授予由此角色定义的权限。有关用于联合身份验证的角色的信息，请参阅《IAM 用户指南》**中的[针对第三方身份提供者创建角色（联合身份验证）](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-idp.html)。如果您使用 IAM Identity Center，则需要配置权限集。为控制您的身份在进行身份验证后可以访问的内容，IAM Identity Center 将权限集与 IAM 中的角色相关联。有关权限集的信息，请参阅《AWS IAM Identity Center 用户指南》**中的[权限集](https://docs.aws.amazon.com/singlesignon/latest/userguide/permissionsetsconcept.html)。

您还可以将 **git-remote-codecommit** 与 IAM 用户结合使用。与其他 HTTPS 连接方法不同，**git-remote-codecommit** 不要求为用户设置 Git 凭证。

**注意**  
有些 IDEs 不支持使用的克隆 URL 格式`git-remote-codecommit`。您可能需要手动将存储库克隆到本地计算机，然后才能在您首选的 IDE 中使用它们。有关更多信息，请参阅 [故障排除 git-remote-codecommit和 AWS CodeCommit](troubleshooting-grc.md)。

编写这些过程时假设您有一个 Amazon Web Services 账户，在中创建了至少一个存储库 CodeCommit，并且在连接到 CodeCommit 存储库时使用具有托管策略的 IAM 用户。有关如何为联合身份用户和其他轮换凭证类型配置访问权限的信息，请参阅[使用轮换凭证连接到 AWS CodeCommit 存储库](temporary-access.md)。

**Topics**
+ [步骤 0：安装必备组件 git-remote-codecommit](#setting-up-git-remote-codecommit-prereq)
+ [步骤 1：的初始配置 CodeCommit](#setting-up-git-remote-codecommit-account)
+ [第 2 步：安装 git-remote-codecommit](#setting-up-git-remote-codecommit-install)
+ [步骤 3：Connect 连接到 CodeCommit 控制台并克隆存储库](#setting-up-git-remote-codecommit-connect-console)
+ [后续步骤](#setting-up-git-remote-codecommit-next-step)

## 步骤 0：安装必备组件 git-remote-codecommit
<a name="setting-up-git-remote-codecommit-prereq"></a>

您必须在本地计算机上安装一些先决条件，然后才能使用 **git-remote-codecommit**。这些方法包括：
+ 支持的 Python 版本。有关支持的 Python 版本的更多信息，请参阅[git-remote-codecommit](https://pypi.org/project/git-remote-codecommit/)。

  有关设置和使用的更多信息 git-remote-codecommit，请参阅[使用的 HTTPS 连接 AWS CodeCommit 的设置步骤 git-remote-codecommit](#setting-up-git-remote-codecommit)。
+ Git

**注意**  
在 Windows 上安装 Python 时，请确保选择了将 Python 添加到路径的选项。

**git-remote-codecommit** 需要 pip 版本 9.0.3 或更高版本。要检查您的 pip 版本，请打开终端或命令行并运行以下命令：

```
pip --version
```

您可以运行以下两个命令将您的 pip 版本更新到最新版本：

```
curl -O https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py --user
```

要处理 CodeCommit 存储库中的文件、提交和其他信息，必须在本地计算机上安装 Git。 CodeCommit 支持 Git 版本 1.7.9 及更高版本。Git 版本 2.28 支持为初始提交配置分支名称。我们建议使用最新版本的 Git。

要安装 Git，建议您访问 [Git 下载](http://git-scm.com/downloads)等网站。

**注意**  
Git 是一个不断发展的平台，会定期进行更新。有时，功能更改可能会影响其工作方式 CodeCommit。如果您在使用特定版本的 Git 时遇到问题 CodeCommit，请查看中的信息[故障排除](troubleshooting.md)。

## 步骤 1：的初始配置 CodeCommit
<a name="setting-up-git-remote-codecommit-account"></a>

请按照以下步骤创建 IAM 用户、使用适当的策略对其进行配置、获取访问密钥和秘密密钥，以及安装和配置 AWS CLI。

**创建和配置用于访问的 IAM 用户 CodeCommit**

1. 前往 [http://aws.amazon.com](https://aws.amazon.com/)，并选择**注册**，创建一个 Amazon Web Services 账户。

1. 创建 IAM 用户或使用您的 Amazon Web Services 账户中的现有用户。确保您具有与该 IAM 用户关联的访问密钥 ID 和秘密访问密钥。有关更多信息，请参阅[在 Amazon Web Services 账户中创建 IAM 用户](https://docs.aws.amazon.com/IAM/latest/UserGuide/Using_SettingUpUser.html)。
**注意**  
CodeCommit 要求 AWS Key Management Service。如果您使用的是现有的 IAM 用户，请确保该用户没有明确拒绝所要求的 AWS KMS 操作的策略 CodeCommit。有关更多信息，请参阅 [AWS KMS 和加密](encryption.md)。

1. 登录 AWS 管理控制台 并打开 IAM 控制台，网址为[https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/)。

1. **在 IAM 控制台的导航窗格中，选择 Users，然后选择要配置 CodeCommit 访问权限的 IAM 用户。**

1. 在 **Permissions** 选项卡上，选择 **Add Permissions**。

1. 在 **Grant permissions (授予权限)** 中，选择 **Attach existing policies directly (直接附加现有策略)**。

1. 从策略列表中，选择**AWSCodeCommitPowerUser**或其他托管策略进行 CodeCommit 访问。有关更多信息，请参阅 [AWS 的托管策略 CodeCommit](security-iam-awsmanpol.md)。

   选择要附加的策略后，选择**下一步：审核**以审核要附加到 IAM 用户的策略列表。如果列表正确，选择 **Add permissions**。

    有关 CodeCommit 托管策略以及与其他群组和用户共享仓库访问权限的更多信息，请参阅[共享存储库](how-to-share-repository.md)和[的身份验证和访问控制 AWS CodeCommit](auth-and-access-control.md)。

**要安装和配置 AWS CLI**

1. 在您的本地计算机上，下载并安装 AWS CLI。这是通过命令行与交互 CodeCommit 的先决条件。我们建议您安装 AWS CLI 版本 2。它是的最新主要版本 AWS CLI ，支持所有最新功能。它是唯一支持使用根账户、联合访问权限或临时证书的版本**git-remote-codecommit**。 AWS CLI 

   有关更多信息，请参阅[使用 AWS 命令行界面进行设置](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html)。
**注意**  
CodeCommit 仅适用于 1.7.38 及更高 AWS CLI 版本。作为最佳实践，请安装或升级 AWS CLI 到可用的最新版本。要确定 AWS CLI 您安装了哪个版本，请运行**aws --version**命令。  
要将旧版本的升级 AWS CLI 到最新版本，请参阅[安装 AWS Command Line Interface](https://docs.aws.amazon.com/cli/latest/userguide/installing.html)。

1. 运行此命令以验证的 CodeCommit 命令 AWS CLI 是否已安装。

   ```
   aws codecommit help
   ```

   此命令返回 CodeCommit 命令列表。

1.  AWS CLI 使用**configure**命令配置配置文件，如下所示：。

   ```
   aws configure
   ```

   出现提示时，指定要与之 AWS 配合使用的 IAM 用户的访问 AWS 密钥和私有访问密钥 CodeCommit。另外，请务必指定存储库的 AWS 区域 存在位置，例如`us-east-2`。系统提示指定默认输出格式时，指定 `json`。例如，如果您正在为 IAM 用户配置相关配置文件：

   ```
   AWS Access Key ID [None]: Type your IAM user AWS access key ID here, and then press Enter
   AWS Secret Access Key [None]: Type your IAM user AWS secret access key here, and then press Enter
   Default region name [None]: Type a supported region for CodeCommit here, and then press Enter
   Default output format [None]: Type json here, and then press Enter
   ```

   有关创建和配置用于的配置文件的更多信息 AWS CLI，请参阅以下内容：
   + [命名配置文件](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)
   + [在中使用 IAM 角色 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-role.html)
   + [设置命令](https://docs.aws.amazon.com/cli/latest/reference/set.html)
   + [使用轮换凭证连接到 AWS CodeCommit 存储库](temporary-access.md)

   要连接到存储库或其他存储库中的资源 AWS 区域，必须 AWS CLI 使用默认的区域名称重新配置。支持的默认区域名称 CodeCommit 包括：
   + us-east-2
   + us-east-1
   + eu-west-1
   + us-west-2
   + ap-northeast-1
   + ap-southeast-1
   + ap-southeast-2
   + ap-southeast-3
   + me-central-1
   + eu-central-1
   + ap-northeast-2
   + sa-east-1
   + us-west-1
   + eu-west-2
   + ap-south-1
   + ap-south-1
   + ca-central-1
   + us-gov-west-1
   + us-gov-east-1
   + eu-north-1
   + ap-east-1
   + me-south-1
   + cn-north-1
   + cn-northwest-1
   + eu-south-1
   + ap-northeast-3
   + af-south-1
   + il-central-1

   有关 CodeCommit 和的更多信息 AWS 区域，请参阅[区域和 Git 连接端点](regions.md)。有关 IAM、访问密钥和秘密密钥的更多信息，请参阅‭[如何获取凭证？](https://docs.aws.amazon.com/IAM/latest/UserGuide/IAM_Introduction.html#IAM_SecurityCredentials)和[管理 IAM 用户的访问密钥](https://docs.aws.amazon.com/IAM/latest/UserGuide/ManagingCredentials.html)。有关 AWS CLI 和配置文件的更多信息，请参阅[命名配置文件](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)。

## 第 2 步：安装 git-remote-codecommit
<a name="setting-up-git-remote-codecommit-install"></a>

请按照以下步骤安装 **git-remote-codecommit**。

**要安装 git-remote-codecommit**

1. 在终端或命令行中，运行以下命令：

   ```
   pip install git-remote-codecommit
   ```
**注意**  
根据操作系统和配置的不同，您可能需要以提升权限（例如 sudo）运行此命令，或使用 --user 参数安装到不需要特殊权限的目录，例如您的当前用户账户。例如，在运行 Linux、macOS 或 Unix 的计算机上：  

   ```
   sudo pip install git-remote-codecommit
   ```
在运行 Windows 的计算机上：  

   ```
   pip install --user git-remote-codecommit
   ```

1. 监控安装过程，直到您看到类似于以下内容的成功消息。

## 步骤 3：Connect 连接到 CodeCommit 控制台并克隆存储库
<a name="setting-up-git-remote-codecommit-connect-console"></a>

如果管理员已经向您发送了**git-remote-codecommit**用于 CodeCommit 存储库的克隆 URL，则可以跳过与控制台的连接，直接克隆存储库。

**连接到存储 CodeCommit 库**

1. 在 [https://console.aws.amazon.com/codesuite/codemmit](https://console.aws.amazon.com/codesuite/codecommit/home) /home 打开 CodeCommit 控制台。

1. 在区域选择器中，选择存储库的创建 AWS 区域 位置。存储库特定于 AWS 区域。有关更多信息，请参阅 [区域和 Git 连接端点](regions.md)。

1. 从列表中找到您要连接的存储库并选择此存储库。选择 **Clone URL (克隆 URL)**，然后选择克隆或连接到存储库时要使用的协议。此时将复制克隆 URL。
   + 如果您使用的是 IAM 用户的 Git 凭证或 AWS CLI附带的凭证助手，请复制 HTTPS URL。
   + 如果您在本地计算机上使用 **git-remote-codecommit** 命令，请复制 HTTPS (GRC) URL。
   + 如果您与 IAM 用户一起使用 SSH public/private 密钥对，请复制 SSH 网址。
**注意**  
 如果您看到的是**欢迎**页面而不是存储库列表，则说明您登录的 AWS 区域 位置中没有与您的 AWS 账户关联的存储库。要创建存储库，请参阅[创建 AWS CodeCommit 存储库](how-to-create-repository.md)或按照[Git 入门和 CodeCommit](getting-started.md)教程中的步骤进行操作。

1. 在终端或命令提示符处，使用 **git clone** 命令克隆存储库。如果您创建了命名 AWS CLI 配置文件，请使用您复制的 HTTPS git-remote-codecommit 网址和配置文件名称。如果未指定配置文件，则该命令将采用默认配置文件。这将在运行命令的目录的子目录中创建本地存储库。例如，要将名为 *MyDemoRepo* 的存储库克隆成名为 *my-demo-repo* 的本地存储库，请运行以下命令：

   ```
   git clone codecommit://MyDemoRepo my-demo-repo
   ```

   要使用名为以下的配置文件克隆同一个存储库*CodeCommitProfile*：

   ```
   git clone codecommit://CodeCommitProfile@MyDemoRepo my-demo-repo
   ```

   要克隆与配置文件中配置的存储库 AWS 区域 不同的存储库，请添加 AWS 区域 名称。例如：

   ```
   git clone codecommit::ap-northeast-1://MyDemoRepo my-demo-repo
   ```

## 后续步骤
<a name="setting-up-git-remote-codecommit-next-step"></a>

您已满足先决条件。按照中的步骤[入门 CodeCommit ](getting-started-cc.md)开始使用 CodeCommit。

要了解如何创建和推送您的第一个提交，请参阅[在中创建提交 AWS CodeCommit](how-to-create-commit.md)。如果您刚刚接触 Git，您可能还需要查看[哪里可以找到有关 Git 的更多信息？](welcome.md#welcome-get-started-with-git)和[Git 入门和 AWS CodeCommit](getting-started.md)中的信息。