

的版本 4 (V4) 适用于 .NET 的 AWS SDK 已经发布！

有关重大更改和迁移应用程序的信息，请参阅[迁移主题](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html)。

 [https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/net-dg-v4.html)

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

# 使用 IAM 角色授予访问权限
<a name="net-dg-hosm"></a>

本教程向您展示如何使用在 Amazon EC2 实例上启用 IAM 角色。 适用于 .NET 的 AWS SDK 

## 概述
<a name="hosm-overview"></a>

对的所有请求都 AWS 必须使用颁发的凭证进行加密签名。 AWS因此，您需要通过某种策略为 Amazon EC2 实例上运行的应用程序管理凭证。您必须以安全的方式分发、存储和轮换这些凭证，并且能让应用程序访问这些凭证。

使用 IAM 角色，您可以有效地管理这些凭证。您可以创建 IAM 角色并使用应用程序所需的权限对其进行配置，然后将该角色附加到 EC2 实例。要详细了解使用 IAM 角色的优势，请参阅 [Amazon EC2 用户指南](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/)中的 [Amazon EC2 的 IAM 角色](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html)。另外，有关 [IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html)的信息，请参阅 IAM 用户指南。

对于使用构建的应用程序 适用于 .NET 的 AWS SDK，当应用程序为 AWS 服务构造客户端对象时，该对象会搜索来自多个潜在来源的证书。它搜索的顺序显示在[凭证和配置文件解析](creds-assign.md)中。

如果客户端对象在任何其它来源中找不到凭证，它会检索与配置为 IAM 角色且位于 EC2 实例元数据中的凭证具有相同权限的临时凭证。这些凭据用于 AWS 从客户端对象对进行调用。

## 关于本教程
<a name="about-hosm-tutorial"></a>

在学习本教程时，您可以使用 适用于 .NET 的 AWS SDK （和其他工具）启动附加了 IAM 角色的 Amazon EC2 实例，然后使用 IAM 角色的权限查看该实例上的应用程序。

**Topics**
+ [概述](#hosm-overview)
+ [关于本教程](#about-hosm-tutorial)
+ [创建示例 Amazon S3 应用程序](#net-dg-hosm-sample-s3-app)
+ [创建一个 IAM 角色](#net-dg-hosm-create-the-role)
+ [启动 EC2 实例并指定 IAM 角色](#net-dg-hosm-launch-ec2-instance)
+ [连接到 EC2 实例](#net-dg-hosm-connect)
+ [在 EC2 实例上运行示例应用程序](#net-dg-hosm-run-the-app)
+ [清理](#net-dg-hosm-cleanup)

## 创建示例 Amazon S3 应用程序
<a name="net-dg-hosm-sample-s3-app"></a>

此示例应用程序从 Amazon S3 中检索对象。要运行示例应用程序，您需要：
+ 包含文本文件的 Amazon S3 桶。
+ AWS 开发计算机上允许您访问存储桶的凭据。

有关创建 Amazon S3 存储桶并上载对象的信息，请参阅 [Amazon Simple Storage Service 用户指南](https://docs.aws.amazon.com/AmazonS3/latest/userguide/)。有关 AWS 证书的信息，请参阅[使用进行身份验证 适用于 .NET 的 AWS SDK AWS](creds-idc.md)。

使用以下代码创建 .NET Core 项目。然后在开发计算机上测试该应用程序。

**注意**  
在您的开发计算机上，安装了 .NET Core 运行时系统，这使您无需发布即可运行应用程序。在本教程中稍后创建 EC2 实例时，您可以选择在实例上安装 .NET Core 运行时系统。这为您提供了类似的体验和较小的文件传输。  
 但是，您也可以选择不在实例上安装 .NET Core 运行时系统。如果您选择此操作方案，则必须发布应用程序，以便在将其转移到实例时包含所有依赖关系。

### SDK 参考
<a name="w2aac19c15c23c25c17c13b1"></a>

NuGet 包裹：
+ [AWSSDK.S3](https://www.nuget.org/packages/AWSSDK.S3)

编程元素：
+ 命名空间 [Amazon.S3](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/S3/NS3.html)

  类 [AmazonS3Client](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/S3/TS3Client.html)
+ 命名空间 [Amazon.S3.Model](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/S3/NS3Model.html)

  班级 [GetObjectResponse](https://docs.aws.amazon.com/sdkfornet/v4/apidocs/items/S3/TGetObjectResponse.html)

### 代码
<a name="w2aac19c15c23c25c17c15b1"></a>

```
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;

namespace S3GetTextItem
{
  // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  // Class to retrieve a text file from an S3 bucket and write it to a local file
  class Program
  {
    static async Task Main(string[] args)
    {
      // Parse the command line and show help if necessary
      var parsedArgs = CommandLine.Parse(args);
      if(parsedArgs.Count == 0)
      {
        PrintHelp();
        return;
      }

      // Get the application arguments from the parsed list
      string bucket =
        CommandLine.GetArgument(parsedArgs, null, "-b", "--bucket-name");
      string item =
        CommandLine.GetArgument(parsedArgs, null, "-t", "--text-object");
      string outFile =
        CommandLine.GetArgument(parsedArgs, null, "-o", "--output-filename");
      if(   string.IsNullOrEmpty(bucket)
         || string.IsNullOrEmpty(item)
         || string.IsNullOrEmpty(outFile))
        CommandLine.ErrorExit(
          "\nOne or more of the required arguments is missing or incorrect." +
          "\nRun the command with no arguments to see help.");

      // Create the S3 client object and get the file object from the bucket.
      var response = await GetObject(new AmazonS3Client(), bucket, item);

      // Write the contents of the file object to the given output file.
      var reader = new StreamReader(response.ResponseStream);
      string contents = reader.ReadToEnd();
      using (var s = new FileStream(outFile, FileMode.Create))
      using (var writer = new StreamWriter(s))
        writer.WriteLine(contents);
    }


    //
    // Method to get an object from an S3 bucket.
    private static async Task<GetObjectResponse> GetObject(
      IAmazonS3 s3Client, string bucket, string item)
    {
        Console.WriteLine($"Retrieving {item} from bucket {bucket}.");
        return await s3Client.GetObjectAsync(bucket, item);
    }


    //
    // Command-line help
    private static void PrintHelp()
    {
      Console.WriteLine(
        "\nUsage: S3GetTextItem -b <bucket-name> -t <text-object> -o <output-filename>" +
        "\n  -b, --bucket-name: The name of the S3 bucket." +
        "\n  -t, --text-object: The name of the text object in the bucket." +
        "\n  -o, --output-filename: The name of the file to write the text to.");
    }
  }


  // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  // Class that represents a command line on the console or terminal.
  // (This is the same for all examples. When you have seen it once, you can ignore it.)
  static class CommandLine
  {
    //
    // Method to parse a command line of the form: "--key value" or "-k value".
    //
    // Parameters:
    // - args: The command-line arguments passed into the application by the system.
    //
    // Returns:
    // A Dictionary with string Keys and Values.
    //
    // If a key is found without a matching value, Dictionary.Value is set to the key
    //  (including the dashes).
    // If a value is found without a matching key, Dictionary.Key is set to "--NoKeyN",
    //  where "N" represents sequential numbers.
    public static Dictionary<string,string> Parse(string[] args)
    {
      var parsedArgs = new Dictionary<string,string>();
      int i = 0, n = 0;
      while(i < args.Length)
      {
        // If the first argument in this iteration starts with a dash it's an option.
        if(args[i].StartsWith("-"))
        {
          var key = args[i++];
          var value = key;

          // Check to see if there's a value that goes with this option?
          if((i < args.Length) && (!args[i].StartsWith("-"))) value = args[i++];
          parsedArgs.Add(key, value);
        }

        // If the first argument in this iteration doesn't start with a dash, it's a value
        else
        {
          parsedArgs.Add("--NoKey" + n.ToString(), args[i++]);
          n++;
        }
      }

      return parsedArgs;
    }

    //
    // Method to get an argument from the parsed command-line arguments
    //
    // Parameters:
    // - parsedArgs: The Dictionary object returned from the Parse() method (shown above).
    // - defaultValue: The default string to return if the specified key isn't in parsedArgs.
    // - keys: An array of keys to look for in parsedArgs.
    public static string GetArgument(
      Dictionary<string,string> parsedArgs, string defaultReturn, params string[] keys)
    {
      string retval = null;
      foreach(var key in keys)
        if(parsedArgs.TryGetValue(key, out retval)) break;
      return retval ?? defaultReturn;
    }

    //
    // Method to exit the application with an error.
    public static void ErrorExit(string msg, int code=1)
    {
      Console.WriteLine("\nError");
      Console.WriteLine(msg);
      Environment.Exit(code);
    }
  }

}
```

如果需要，可以临时删除在开发计算机上使用的凭证，以查看应用程序的响应情况。（但请务必在完成后恢复凭证。）

## 创建一个 IAM 角色
<a name="net-dg-hosm-create-the-role"></a>

创建具有合适权限以访问 Amazon S3 的 IAM 角色。

1. 打开 [IAM 控制台](https://console.aws.amazon.com/iam/)。

1. 在导航窗格中，选择**角色**，然后选择**创建角色**。

1. 选择 **AWS 服务**，找到并选择 **EC2**，然后选择**下一步: 权限**。

1. 在 “**附加权限策略**” 下，找到并选择 **AmazonS3 ReadOnlyAccess**。如果愿意，请查看该策略，然后选择**下一步: 标签**。

1. 如果需要，可以添加标签，然后选择**下一步: 查看**。

1. 键入角色的名称和描述，然后选择**创建角色**。请记住此名称，因为在启动 EC2 实例时会用到它。

## 启动 EC2 实例并指定 IAM 角色
<a name="net-dg-hosm-launch-ec2-instance"></a>

使用先前创建的 IAM 角色启动 EC2 实例。您可以通过下列方式来执行此操作。
+ **使用 EC2 控制台**

  要使用 EC2 控制台启动实例，请参阅 [Amazon EC2 用户指南](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/)中的[使用新的启动实例向导启动实例](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-instance-wizard.html)。

  浏览启动页面时，您至少应展开**高级详细信息**窗格，以便指定之前在 **IAM 实例配置文件**中创建的 IAM 角色。
+ **使用 适用于 .NET 的 AWS SDK**

  有关此操作的信息，请参阅[启动 Amazon EC2 实例](run-instance.md)，包括该主题接近末尾处的[其他注意事项](run-instance.md#run-instance-additional)。

要启动附加了 IAM 角色的 EC2 实例，IAM 用户的配置必须包含某些权限。有关所需权限的更多信息，请参阅 [Amazon EC2 用户指南](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/)中的[授予用户将 IAM 角色传递给实例的权限](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#permission-to-pass-iam-roles)。

## 连接到 EC2 实例
<a name="net-dg-hosm-connect"></a>

连接到 EC2 实例，以便您可将示例应用程序传输到该实例，然后运行该应用程序。您将需要包含用于启动实例的密钥对私有部分的文件；即 PEM 文件。

有关如何连接到实例的信息，请参阅 [Amazon EC2 用户指南](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/)中的[连接到 Linux 实例](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connect-to-linux-instance.html)或[连接到 Windows 实例](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/connecting_to_windows_instance.html)。当您连接时，请确保您可以将文件从开发计算机传输到您的实例。

如果您在 Windows 上使用 Visual Studio，也可以使用 Toolkit for Visual Studio 连接到实例。有关更多信息，请参阅 AWS Toolkit for Visual Studio 用户指南中的[连接到 Amazon EC2 实例](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/tkv-ec2-ami.html#connect-ec2)。

## 在 EC2 实例上运行示例应用程序
<a name="net-dg-hosm-run-the-app"></a>

1. 将应用程序从本地驱动器复制到您的实例。

   传输哪些文件取决于您如何构建应用程序以及您的实例是否安装了 .NET Core 运行时系统。有关如何将文件传输到实例的信息，请参阅 [Amazon EC2 用户指南](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/)中的[连接到 Linux 实例](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connect-to-linux-instance.html)（请参阅相应的子部分）或[将文件传输到 Windows 实例](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connect-to-linux-instanceWindowsFileTransfer.html)。

1. 启动应用程序并验证其运行结果是否与开发计算机上的结果相同。

1. 验证应用程序是否使用 IAM 角色提供的凭证。

   1. 打开 [Amazon EC2 控制台](https://console.aws.amazon.com/ec2/)。

   1. 选择实例，然后通过**操作**、**实例设置**和**附加/替换 IAM 角色**分离 IAM 角色。

   1. 再次运行该应用程序，看看它是否返回了授权错误。

## 清理
<a name="net-dg-hosm-cleanup"></a>

完成本教程后，如果您不再需要自己创建的 EC2 实例，请务必终止该实例以避免不必要的开支。您可以在 [Amazon EC2 控制台](https://console.aws.amazon.com/ec2/)中或以编程方式执行此操作，如[终止 Amazon EC2 实例](terminate-instance.md)中所述。您还可以删除为本教程创建的其它资源。其中可能包括 IAM 角色、EC2 密钥对和 PEM 文件、安全组等。