

# 设置用于演练的工具
<a name="policy-eval-walkthrough-download-awscli"></a>

介绍性示例（请参阅 [演练：使用策略管理针对 Amazon S3 资源的访问权限](example-walkthroughs-managing-access.md)）使用 AWS 管理控制台 来创建资源和授予权限。为了测试权限，这些示例使用命令行工具 AWS Command Line Interface（AWS CLI）和 AWS Tools for Windows PowerShell，因此您无需编写任何代码。要测试权限，您必须设置其中的一个工具。

**设置 AWS CLI**

1. 下载并配置 AWS CLI。有关说明，请参阅《AWS Command Line Interface用户指南》**中的以下主题：

    [安装或更新到最新版本的 AWS Command Line Interface](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html) 

    [开始使用 AWS Command Line Interface](https://docs.aws.amazon.com/cli/latest/userguide/installing.html) 

1. 设置默认的配置文件。

   您将用户凭证存储在 AWS CLI config 文件中。使用 AWS 账户凭证在配置文件中创建默认配置。有关查找和编辑 AWS CLI 配置文件的说明，请参阅 [Configuration and credential file settings](https://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html)。

   ```
   [default]
   aws_access_key_id = access key ID
   aws_secret_access_key = secret access key
   region = us-west-2
   ```

1. 在命令提示符处输入以下命令来验证设置。这两个命令没有显式提供凭证，因此将使用默认配置的凭证。
   + 尝试 `help` 命令。

     ```
     aws help
     ```
   + 要获取所配置账户的存储桶列表，请使用 `aws s3 ls` 命令。

     ```
     aws s3 ls
     ```

当您进行演练时，您将创建用户，并且通过创建配置将用户凭证保存在配置文件中，如以下示例所示。这些配置文件的名称为 `AccountAadmin` 和 `AccountBadmin`。

```
[profile AccountAadmin]
aws_access_key_id = User AccountAadmin access key ID
aws_secret_access_key = User AccountAadmin secret access key
region = us-west-2

[profile AccountBadmin]
aws_access_key_id = Account B access key ID
aws_secret_access_key = Account B secret access key
region = us-east-1
```

要使用这些用户凭证来运行命令，请添加 `--profile` 参数来指定配置文件名称。以下 AWS CLI 命令检索 *`examplebucket`* 中对象的列表并指定 `AccountBadmin` 配置文件。

```
aws s3 ls s3://examplebucket --profile AccountBadmin
```

或者，您也可以从命令提示符处更改 `AWS_DEFAULT_PROFILE` 环境变量，将一组用户凭证配置为默认的配置文件。完成此操作后，当执行不带 `--profile` 参数的 AWS CLI 命令时，AWS CLI 会将您在环境变量中设置的配置文件用作默认配置文件。

```
$ export AWS_DEFAULT_PROFILE=AccountAadmin
```

**设置 AWS Tools for Windows PowerShell**

1. 下载并配置 AWS Tools for Windows PowerShell。有关说明，请参阅**《AWS Tools for PowerShell User Guide》中的 [Installing the AWS Tools for Windows PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-getting-set-up.html#pstools-installing-download)。
**注意**  
要加载 AWS Tools for Windows PowerShell 模块，必须启用 PowerShell 脚本执行。有关更多信息，请参阅**《AWS Tools for PowerShell User Guide》中的 [Enable Script Execution](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-getting-set-up.html#enable-script-execution)。

1. 对于这些演练，您需使用 `Set-AWSCredentials` 命令指定每个会话的 AWS 凭证。该命令会将凭证保存到持久存储 (`-StoreAs ` 参数)。

   ```
   Set-AWSCredentials -AccessKey AccessKeyID -SecretKey SecretAccessKey -storeas string
   ```

1. 验证设置。
   + 要检索可用于 Amazon S3 操作的命令的列表，请运行 `Get-Command` 命令。

     ```
     Get-Command -module awspowershell -noun s3* -StoredCredentials string
     ```
   + 要检索存储桶中的对象的列表，请运行 `Get-S3Object` 命令。

     ```
     Get-S3Object -BucketName bucketname -StoredCredentials string
     ```

有关命令列表，请参阅 [AWS Tools for PowerShell Cmdlet Reference](https://docs.aws.amazon.com/powershell/latest/reference/Index.html)。

现在您已经准备就绪，可以尝试进行演练了。访问每个部分开头提供的链接。