

第 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)

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

# 開始使用 適用於 .NET 的 AWS SDK
開始使用

了解如何安裝、設定和使用 適用於 .NET 的 AWS SDK。您必須安裝工具鏈，並設定應用程式存取 AWS 服務所需的許多基本項目。其中包含：
+ 適當的使用者帳戶或角色
+ 該使用者帳戶或 擔任該角色的身分驗證資訊

如需如何設定專案和其他關鍵組件的資訊，請參閱 [設定 適用於 .NET 的 AWS SDK](configuring-the-sdk.md)。

**Topics**
+ [安裝和設定您的工具鏈](net-dg-dev-env.md)
+ [使用 驗證 AWS](creds-idc.md)
+ [建立簡單的應用程式](quick-start.md)

# 安裝和設定 的工具鏈 適用於 .NET 的 AWS SDK
安裝和設定您的工具鏈

若要使用 適用於 .NET 的 AWS SDK，您必須安裝特定開發工具。

## 跨平台開發


在 Windows、Linux 或 macOS 上進行跨平台 .NET 開發時需要下列項目：
+ Microsoft [.NET Core 軟體開發套件](https://learn.microsoft.com/en-us/dotnet/fundamentals/) 2.1 版、3.1 版或更新版本，包括 .NET 命令列界面 (CLI) (**`dotnet`**) 以及 .NET Core 執行時間。
+ 適合您作業系統和需求的程式碼編輯器或整合式開發環境 (IDE)。這通常是為 .NET Core 提供一些支援。

  舉例來說，包括 [Microsoft Visual Studio 程式碼 (VS 程式碼)](https://code.visualstudio.com/)、[JetBrains Rider](https://www.jetbrains.com/rider/) 和 [Microsoft Visual Studio](https://visualstudio.microsoft.com/vs/)。
+ （選用） 如果 AWS 工具組可用於您選擇的編輯器和作業系統。

  範例包括 [AWS Toolkit for Visual Studio Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html)、 [AWS Toolkit for JetBrains](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)和 [AWS Toolkit for Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html)。

## Windows 搭配 Visual Studio 和 .NET Core


在 Windows 上使用 Visual Studio 和 .NET Core 進行開發時需要下列項目：
+ [Microsoft Visual Studio](https://visualstudio.microsoft.com/vs/)
+ Microsoft .NET Core 2.1、3.1 或更新版本

  根據預設，安裝最新版本的 Visual Studio 時通常會包含此項目。
+ （選用） 是一種 AWS Toolkit for Visual Studio外掛程式，提供用於從 Visual Studio 管理 AWS 資源和本機設定檔的使用者介面。若要安裝工具組，請參閱[設定 AWS Toolkit for Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/getting-set-up.html)。

  如需詳細資訊，請參閱[「AWS Toolkit for Visual Studio 使用者指南」](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/)。

## 下一步驟


[適用於 .NET 的 AWS SDK 使用 驗證 AWS](creds-idc.md)

# 適用於 .NET 的 AWS SDK 使用 驗證 AWS
使用 驗證 AWS

在使用 進行開發 AWS 時，您必須建立程式碼向 進行身分驗證的方式 AWS 服務。視環境和您可用的存取權而定，您可以透過不同方式設定 AWS 資源的程式設計 AWS 存取。

若要查看 SDK 的各種身分驗證方法，請參閱 *AWS SDKs和工具參考指南中的*[身分驗證和存取](https://docs.aws.amazon.com/sdkref/latest/guide/access.html)。

本主題假設新的使用者正在本機開發，其雇主尚未提供身分驗證方法，且將使用 AWS IAM Identity Center 取得臨時登入資料。如果您的環境不適用這些假設，則本主題中的有些資訊可能不適用您的環境，或者有些資訊可能已經提供給您。

 設定此環境需要幾個步驟，總結如下：

1. [使用主控台登入資料登入](#login-con-creds)

1. [啟用和設定 IAM Identity Center](#idc-config-sso)

1. [設定 SDK 以使用 IAM Identity Center。](#idc-config-sdk)

1. [啟動 AWS 存取入口網站工作階段](#idc-start-portal)

## 使用主控台登入資料登入


您可以使用現有的 AWS 管理主控台登入憑證，以程式設計方式存取 AWS 服務。在瀏覽器型身分驗證流程之後， AWS 會產生臨時登入資料，可用於 CLI AWS AWS Tools for PowerShell 和適用於 .NET v4 的 AWS SDK 等本機開發工具。

透過此程序，您可以使用在初始帳戶設定期間建立的根登入資料、IAM 使用者或來自身分提供者的聯合身分，以及 AWS CLI 進行身分驗證，或 AWS Tools for PowerShell 自動為您管理臨時登入資料。這種方法無需在本機存放長期登入資料，可增強安全性。當您從 AWS Tools for PowerShell 執行 `Invoke-AWSLogin ` cmdlet 或從 CLI AWS 執行 `aws login`命令時，您可以從作用中主控台工作階段中選取 ，或透過瀏覽器型身分驗證流程登入，這樣會自動產生臨時登入資料。臨時登入資料字符將在 15 分鐘內過期，但適用於 .NET v4 的 AWS SDK 會在請求期間視需要自動重新整理字符。

**重要**  
如果您使用主控台登入資料進行身分驗證，您的應用程式必須參考下列 NuGet 套件，以便 AWS 登入解析能夠運作：  
**AWSSDK.Signin**
未參考這些套件將導致執行時間例外狀況。
+ [AWS Tools for PowerShell login creds 開發人員指南。](https://docs.aws.amazon.com/powershell/v5/userguide/creds-idc.html#login-con-creds)
+ [使用 AWS CLI 使用主控台登入資料登入 AWS 進行本機開發。](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sign-in.html)

按照上述為 AWS Tools for PowerShell 或 CLI AWS 提供的步驟登入後， `config` 檔案看起來會像這樣。

```
[default]
login_session = arn:aws:iam::0123456789012:user/username
region = us-east-1

[my-dev-profile]
login_session = arn:aws:iam::0123456789012:user/username
region = us-east-1
```

## 啟用和設定 IAM Identity Center


若要使用 IAM Identity Center，必須先啟用和設定它。若要查看如何為 SDK 執行此操作的詳細資訊，請參閱 SDK **和工具參考指南中的 IAM Identity Center 身分驗證主題中的步驟 1**。 [https://docs.aws.amazon.com/sdkref/latest/guide/access-sso.html](https://docs.aws.amazon.com/sdkref/latest/guide/access-sso.html) *AWS SDKs * 具體來說，請遵循**我沒有透過 IAM Identity Center 建立存取權限**下的任何必要說明。

## 設定 SDK 以使用 IAM Identity Center。


有關如何設定 SDK 以使用 IAM Identity Center 的資訊，請參閱 SDK *AWS SDKs 和工具參考指南*中 [IAM Identity Center 身分驗證](https://docs.aws.amazon.com/sdkref/latest/guide/access-sso.html)主題**的步驟 2**。完成此組態之後，您的系統應該包含下列元素：
+ 在執行應用程式之前 AWS CLI，您用來啟動 AWS 存取入口網站工作階段的 。
+ 包含[`[default]`設定檔](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html#file-format-profile)的共用 AWS `config`檔案，其中包含一組可從 SDK 參考的組態值。若要尋找此檔案的位置，請參閱 *AWS SDK 和工具參考指南*中的[共用檔案位置](https://docs.aws.amazon.com/sdkref/latest/guide/file-location.html)。 適用於 .NET 的 AWS SDK 使用設定檔的 SSO 權杖提供者，在傳送請求至 之前取得登入資料 AWS。該 `sso_role_name` 值是連接到 IAM Identity Center 許可集合的 IAM 角色，應該允許存取應用程式中使用的 AWS 服務 。

  下列範例 `config` 檔案顯示使用 SSO 權杖提供者設定的預設設定檔。設定檔的 `sso_session` 設定是指已命名的 `sso-session` 區段。`sso-session` 區段包含啟動 AWS 存取入口網站工作階段的設定。

  ```
  [default]
  sso_session = my-sso
  sso_account_id = 111122223333
  sso_role_name = SampleRole
  region = us-east-1
  output = json
  
  [sso-session my-sso]
  sso_region = us-east-1
  sso_start_url = https://provided-domain.awsapps.com/start
  sso_registration_scopes = sso:account:access
  ```

**重要**  
如果您使用 AWS IAM Identity Center 進行身分驗證，您的應用程式必須參考下列 NuGet 套件，以便 SSO 解析能夠運作：  
`AWSSDK.SSO`
`AWSSDK.SSOOIDC`
未參考這些套件將導致*執行時間*例外狀況。

## 啟動 AWS 存取入口網站工作階段


在執行存取的應用程式之前 AWS 服務，您需要 SDK 的作用中 AWS 存取入口網站工作階段，才能使用 IAM Identity Center 身分驗證來解析登入資料。視您設定的工作階段長度而定，您的存取最終會過期，且 SDK 將遇到身分驗證錯誤。若要登入 AWS 存取入口網站，請在 中執行下列命令 AWS CLI。

```
aws sso login
```

由於您有預設的設定檔設定，因此您不需要使用 `--profile` 選項呼叫指令。如果您的 SSO 權杖提供者組態使用已命名的設定檔，則命令為 `aws sso login --profile named-profile`。

若要測試您是否已經有作用中的工作階段，請執行下列 AWS CLI 命令。

```
aws sts get-caller-identity
```

對此命令的回應，應報告共用 `config` 檔案中設定的 IAM Identity Center 帳戶和許可集合。

**注意**  
如果您已有作用中的 AWS 存取入口網站工作階段並執行 `aws sso login`，則不需要提供登入資料。  
登入程序可能會提示您允許 AWS CLI 存取您的資料。由於 AWS CLI 建置在適用於 Python 的 SDK 之上，因此許可訊息可能包含`botocore`名稱的變化。

## 其他資訊

+ 如需在開發環境中使用 IAM Identity Center 和 SSO 的詳細資訊，請參閱 [進階身分驗證](advanced-auth.md) 一節[單一登入](sso.md)中的 。此資訊包括替代和更進階的方法，以及向您展示如何使用這些方法的教學課程。
+ 如需 SDK 身分驗證的更多選項，例如使用設定檔和環境變數，請參閱 *AWS SDKs和工具參考指南*中的[組態](https://docs.aws.amazon.com/sdkref/latest/guide/creds-config-files.html)章節。
+ 如需了解有關最佳實務的資訊，請參閱 *IAM 使用者指南*中的 [IAM 安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。
+ 若要建立短期 AWS 登入資料，請參閱《*IAM 使用者指南*》中的[暫時安全登入](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html)資料。
+ 若要瞭解其他憑證提供者，請參閱 *AWS SDK 和工具參考指南*中的[標準化憑證提供者](https://docs.aws.amazon.com/sdkref/latest/guide/standardized-credentials.html)。

# 使用 建立簡單的應用程式 適用於 .NET 的 AWS SDK
建立簡單的應用程式

本節為初次使用 的開發人員提供基本教學課程 適用於 .NET 的 AWS SDK。

**注意**  
使用這些教學課程之前，您必須先[安裝工具鏈](net-dg-dev-env.md)並[設定 SDK 身分驗證](creds-idc.md)。

如需為特定 AWS 服務開發軟體以及程式碼範例的詳細資訊，請參閱 [呼叫 AWS 服務](working-with-aws-services.md)。如需其他程式碼範例，請參閱 [適用於 .NET 的 SDK (v4) 程式碼範例](csharp_code_examples.md)。

**Topics**
+ [簡單的跨平台應用程式](quick-start-s3-1-cross.md)
+ [簡單的 Windows 型應用程式](quick-start-s3-1-winvs.md)
+ [

# 後續步驟
](quick-start-next-steps.md)

# 使用 的簡單跨平台應用程式 適用於 .NET 的 AWS SDK
簡單的跨平台應用程式

本教學課程使用 適用於 .NET 的 AWS SDK 和 .NET Core 進行跨平台開發。本教學課程說明如何使用 SDK 列出您擁有的 [Amazon S3 儲存貯](https://docs.aws.amazon.com/AmazonS3/latest/userguide/)體，並選擇性地建立儲存貯體。

您將使用 .NET 命令列界面 (CLI) 等跨平台工具來執行此教學課程。如需設定開發環境的其他方式，請參閱 [安裝和設定 的工具鏈 適用於 .NET 的 AWS SDK](net-dg-dev-env.md)。

**在 Windows、Linux 或 macOS 上進行跨平台 .NET 開發時需要：**
+ Microsoft [.NET Core 軟體開發套件](https://learn.microsoft.com/en-us/dotnet/fundamentals/) 2.1 版、3.1 版或更新版本，包括 .NET 命令列界面 (CLI) (**`dotnet`**) 以及 .NET Core 執行時間。
+ 程式碼編輯器或整合式開發環境 (IDE)，適用於您的作業系統和需求。這通常是為 .NET Core 提供一些支援。

  舉例來說，包括 [Microsoft Visual Studio 程式碼 (VS 程式碼)](https://code.visualstudio.com/)、[JetBrains Rider](https://www.jetbrains.com/rider/) 和 [Microsoft Visual Studio](https://visualstudio.microsoft.com/vs/)。

**注意**  
使用這些教學課程之前，您必須先[安裝工具鏈](net-dg-dev-env.md)並[設定 SDK 身分驗證](creds-idc.md)。

## 步驟

+ [建立專案](#s3-1-cross-create-project)
+ [建立程式碼](#s3-1-cross-code)
+ [執行應用程式](#s3-1-cross-run)
+ [清除](#s3-1-cross-clean-up)

## 建立專案


1. 開啟命令提示字元或終端機。尋找或建立可在其下建立 .NET 專案的作業系統資料夾。

1. 在該資料夾中，執行下列命令以建立 .NET 專案。

   ```
   dotnet new console --name S3CreateAndList
   ```

1. 移至新建立的`S3CreateAndList`資料夾並執行下列命令：

   ```
   dotnet add package AWSSDK.S3
   dotnet add package AWSSDK.SecurityToken
   dotnet add package AWSSDK.SSO
   dotnet add package AWSSDK.SSOOIDC
   ```

   上述命令會從 NuGet 套件[管理員安裝 NuGet 套件](https://www.nuget.org/profiles/awsdotnet)。由於我們確切知道本教學課程所需的 NuGet 套件，因此現在可以執行此步驟。在開發期間，所需的套件也很常見。發生這種情況時，即可執行類似的命令。

## 建立程式碼


1. 在 `S3CreateAndList` 資料夾中，在程式碼編輯器中尋找並開啟 `Program.cs`。

1. 以下列程式碼取代內容，並儲存檔案。

   ```
   using System;
   using System.Threading.Tasks;
   
   // NuGet packages: AWSSDK.S3, AWSSDK.SecurityToken, AWSSDK.SSO, AWSSDK.SSOOIDC
   using Amazon.Runtime;
   using Amazon.Runtime.CredentialManagement;
   using Amazon.S3;
   using Amazon.S3.Model;
   using Amazon.SecurityToken;
   using Amazon.SecurityToken.Model;
   
   namespace S3CreateAndList
   {
       class Program
       {
           // This code is part of the quick tour in the developer guide.
           // See https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/quick-start.html
           // for complete steps.
           // Requirements:
           // - An SSO profile in the SSO user's shared config file with sufficient privileges for
   		//   STS and S3 buckets.
           // - An active SSO Token.
           //    If an active SSO token isn't available, the SSO user should do the following:
           //    In a terminal, the SSO user must call "aws sso login".
   
           // Class members.
           static async Task Main(string[] args)
           {
               // Get SSO credentials from the information in the shared config file.
               // For this tutorial, the information is in the [default] profile.
               var ssoCreds = LoadSsoCredentials("default");
   
               // Display the caller's identity.
               var ssoProfileClient = new AmazonSecurityTokenServiceClient(ssoCreds);
               Console.WriteLine($"\nSSO Profile:\n {await ssoProfileClient.GetCallerIdentityArn()}");
   
               // Create the S3 client is by using the SSO credentials obtained earlier.
               var s3Client = new AmazonS3Client(ssoCreds);
   
               // Parse the command line arguments for the bucket name.
               if (GetBucketName(args, out String bucketName))
               {
                   // If a bucket name was supplied, create the bucket.
                   // Call the API method directly
                   try
                   {
                       Console.WriteLine($"\nCreating bucket {bucketName}...");
                       var createResponse = await s3Client.PutBucketAsync(bucketName);
                       Console.WriteLine($"Result: {createResponse.HttpStatusCode.ToString()}");
                   }
                   catch (Exception e)
                   {
                       Console.WriteLine("Caught exception when creating a bucket:");
                       Console.WriteLine(e.Message);
                   }
               }
   
               // Display a list of the account's S3 buckets.
               Console.WriteLine("\nGetting a list of your buckets...");
               var listResponse = await s3Client.ListBucketsAsync();
               Console.WriteLine($"Number of buckets: {listResponse.Buckets.Count}");
               foreach (S3Bucket b in listResponse.Buckets)
               {
                   Console.WriteLine(b.BucketName);
               }
               Console.WriteLine();
           }
   
           // 
           // Method to parse the command line.
           private static Boolean GetBucketName(string[] args, out String bucketName)
           {
               Boolean retval = false;
               bucketName = String.Empty;
               if (args.Length == 0)
               {
                   Console.WriteLine("\nNo arguments specified. Will simply list your Amazon S3 buckets." +
                     "\nIf you wish to create a bucket, supply a valid, globally unique bucket name.");
                   bucketName = String.Empty;
                   retval = false;
               }
               else if (args.Length == 1)
               {
                   bucketName = args[0];
                   retval = true;
               }
               else
               {
                   Console.WriteLine("\nToo many arguments specified." +
                     "\n\ndotnet_tutorials - A utility to list your Amazon S3 buckets and optionally create a new one." +
                     "\n\nUsage: S3CreateAndList [bucket_name]" +
                     "\n - bucket_name: A valid, globally unique bucket name." +
                     "\n - If bucket_name isn't supplied, this utility simply lists your buckets.");
                   Environment.Exit(1);
               }
               return retval;
           }
   
           //
           // Method to get SSO credentials from the information in the shared config file.
           static AWSCredentials LoadSsoCredentials(string profile)
           {
               var chain = new CredentialProfileStoreChain();
               if (!chain.TryGetAWSCredentials(profile, out var credentials))
                   throw new Exception($"Failed to find the {profile} profile");
               return credentials;
           }
       }
   
       // Class to read the caller's identity.
       public static class Extensions
       {
           public static async Task<string> GetCallerIdentityArn(this IAmazonSecurityTokenService stsClient)
           {
               var response = await stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest());
               return response.Arn;
           }
       }
   }
   ```

## 執行應用程式


1. 執行下列命令。

   ```
   dotnet run
   ```

1. 檢查輸出以查看您擁有的 Amazon S3 儲存貯體數量，如果有的話，及其名稱。

1. 選擇新 Amazon S3 儲存貯體的名稱。使用「dotnet-quicktour-s3-1-cross-」作為基礎，並為其新增唯一項目，例如 GUID 或您的名稱。請務必遵循儲存貯體名稱的規則，如 [Amazon S3 使用者指南](https://docs.aws.amazon.com/AmazonS3/latest/userguide/)中的[儲存貯體命名規則](https://docs.aws.amazon.com/AmazonS3/latest/userguide/BucketRestrictions.html#bucketnamingrules)所述。

1. 執行下列命令，以您選擇的儲存貯體名稱取代 *amzn-s3-demo-bucket*。

   ```
   dotnet run amzn-s3-demo-bucket
   ```

1. 檢查輸出以查看建立的新儲存貯體。

## 清除


執行本教學課程時，您建立了一些資源，此時可以選擇清除。
+ 如果您不想保留應用程式在先前步驟中建立的儲存貯體，請使用 Amazon S3 主控台將其刪除，網址為 https：//[https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/)。
+ 如果您不想保留 .NET 專案，請從開發環境中刪除該 `S3CreateAndList` 資料夾。

## 後續作業


返回[快速導覽功能表](quick-start.md)或直接前往[此快速導覽的結尾](quick-start-next-steps.md)。

# 使用 的簡易 Windows 應用程式 適用於 .NET 的 AWS SDK
簡單的 Windows 型應用程式

本教學課程使用 Windows 適用於 .NET 的 AWS SDK 上的 搭配 Visual Studio 和 .NET Core。本教學課程說明如何使用 SDK 列出您擁有的 [Amazon S3 儲存貯](https://docs.aws.amazon.com/AmazonS3/latest/userguide/)體，並選擇性地建立儲存貯體。

您將使用 Visual Studio 和 .NET Core 在 Windows 上執行此教學課程。如需設定開發環境的其他方式，請參閱 [安裝和設定 的工具鏈 適用於 .NET 的 AWS SDK](net-dg-dev-env.md)。

**在 Windows 上使用 Visual Studio 和 .NET Core 進行開發時需要：**
+ [Microsoft Visual Studio](https://visualstudio.microsoft.com/vs/)
+ Microsoft .NET Core 2.1、3.1 或更新版本

  在安裝最新版本的 Visual Studio 時，這通常是預設包含的。

**注意**  
使用這些教學課程之前，您必須先[安裝工具鏈](net-dg-dev-env.md)並[設定 SDK 身分驗證](creds-idc.md)。

## 步驟

+ [建立專案](#s3-1-winvs-create-project)
+ [建立程式碼](#s3-1-winvs-code)
+ [執行應用程式](#s3-1-winvs-run)
+ [清除](#s3-1-winvs-clean-up)

## 建立專案


1. 開啟 Visual Studio 並建立新的專案，使用**主控台應用程式**範本的 C\$1 版本；亦即，描述為「...用於建立可在 .NET... 上執行的命令列應用程式」。命名專案 `S3CreateAndList`。
**注意**  
請勿選擇主控台應用程式範本的 .NET Framework 版本，或者，如果您選擇，請務必使用 .NET Framework 4.7.2 或更新版本。

1. 載入新建立的專案後，選擇**工具**、**NuGet Package Manager**、**管理 NuGet Packages for Solution**。

1. 瀏覽下列 NuGet 套件，並將其安裝到專案中：`AWSSDK.S3`、`AWSSDK.SSO`、 `AWSSDK.SecurityToken`和 `AWSSDK.SSOOIDC`

   此程序會從 NuGet 套件[管理員安裝 NuGet 套件](https://www.nuget.org/profiles/awsdotnet)。由於我們確切知道本教學課程所需的 NuGet 套件，因此現在可以執行此步驟。在開發期間，所需的套件也很常見。發生這種情況時，請按照類似的過程進行安裝。

1. 如果您想要從命令提示字元執行應用程式，請現在開啟命令提示字元，然後導覽至將包含建置輸出的資料夾。這通常類似 `S3CreateAndList\S3CreateAndList\bin\Debug\net6.0`，但取決於您的環境。

## 建立程式碼


1. 在 `S3CreateAndList` 專案中，尋找並在 IDE 中開啟 `Program.cs`。

1. 以下列程式碼取代內容，並儲存檔案。

   ```
   using System;
   using System.Threading.Tasks;
   
   // NuGet packages: AWSSDK.S3, AWSSDK.SecurityToken, AWSSDK.SSO, AWSSDK.SSOOIDC
   using Amazon.Runtime;
   using Amazon.Runtime.CredentialManagement;
   using Amazon.S3;
   using Amazon.S3.Model;
   using Amazon.SecurityToken;
   using Amazon.SecurityToken.Model;
   
   namespace S3CreateAndList
   {
       class Program
       {
           // This code is part of the quick tour in the developer guide.
           // See https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/quick-start.html
           // for complete steps.
           // Requirements:
           // - An SSO profile in the SSO user's shared config file with sufficient privileges for
   		//   STS and S3 buckets.
           // - An active SSO Token.
           //    If an active SSO token isn't available, the SSO user should do the following:
           //    In a terminal, the SSO user must call "aws sso login".
   
           // Class members.
           static async Task Main(string[] args)
           {
               // Get SSO credentials from the information in the shared config file.
               // For this tutorial, the information is in the [default] profile.
               var ssoCreds = LoadSsoCredentials("default");
   
               // Display the caller's identity.
               var ssoProfileClient = new AmazonSecurityTokenServiceClient(ssoCreds);
               Console.WriteLine($"\nSSO Profile:\n {await ssoProfileClient.GetCallerIdentityArn()}");
   
               // Create the S3 client is by using the SSO credentials obtained earlier.
               var s3Client = new AmazonS3Client(ssoCreds);
   
               // Parse the command line arguments for the bucket name.
               if (GetBucketName(args, out String bucketName))
               {
                   // If a bucket name was supplied, create the bucket.
                   // Call the API method directly
                   try
                   {
                       Console.WriteLine($"\nCreating bucket {bucketName}...");
                       var createResponse = await s3Client.PutBucketAsync(bucketName);
                       Console.WriteLine($"Result: {createResponse.HttpStatusCode.ToString()}");
                   }
                   catch (Exception e)
                   {
                       Console.WriteLine("Caught exception when creating a bucket:");
                       Console.WriteLine(e.Message);
                   }
               }
   
               // Display a list of the account's S3 buckets.
               Console.WriteLine("\nGetting a list of your buckets...");
               var listResponse = await s3Client.ListBucketsAsync();
               Console.WriteLine($"Number of buckets: {listResponse.Buckets.Count}");
               foreach (S3Bucket b in listResponse.Buckets)
               {
                   Console.WriteLine(b.BucketName);
               }
               Console.WriteLine();
           }
   
           // 
           // Method to parse the command line.
           private static Boolean GetBucketName(string[] args, out String bucketName)
           {
               Boolean retval = false;
               bucketName = String.Empty;
               if (args.Length == 0)
               {
                   Console.WriteLine("\nNo arguments specified. Will simply list your Amazon S3 buckets." +
                     "\nIf you wish to create a bucket, supply a valid, globally unique bucket name.");
                   bucketName = String.Empty;
                   retval = false;
               }
               else if (args.Length == 1)
               {
                   bucketName = args[0];
                   retval = true;
               }
               else
               {
                   Console.WriteLine("\nToo many arguments specified." +
                     "\n\ndotnet_tutorials - A utility to list your Amazon S3 buckets and optionally create a new one." +
                     "\n\nUsage: S3CreateAndList [bucket_name]" +
                     "\n - bucket_name: A valid, globally unique bucket name." +
                     "\n - If bucket_name isn't supplied, this utility simply lists your buckets.");
                   Environment.Exit(1);
               }
               return retval;
           }
   
           //
           // Method to get SSO credentials from the information in the shared config file.
           static AWSCredentials LoadSsoCredentials(string profile)
           {
               var chain = new CredentialProfileStoreChain();
               if (!chain.TryGetAWSCredentials(profile, out var credentials))
                   throw new Exception($"Failed to find the {profile} profile");
               return credentials;
           }
       }
   
       // Class to read the caller's identity.
       public static class Extensions
       {
           public static async Task<string> GetCallerIdentityArn(this IAmazonSecurityTokenService stsClient)
           {
               var response = await stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest());
               return response.Arn;
           }
       }
   }
   ```

1. 建置應用程式。
**注意**  
如果您使用的是較舊版本的 Visual Studio，您可能會收到類似以下的建置錯誤：  
C\$1 7.0 不提供「功能」非同步主節點」。請使用語言版本 7.1 或更新版本。」  
如果您收到此錯誤，請將您的專案設定為使用較新版本的語言。這通常在專案屬性、**建置**、**進階**中完成。

## 執行應用程式


1. 執行沒有命令列引數的應用程式。在命令提示字元 （如果您先前已開啟） 或從 IDE 中執行此操作。

1. 檢查輸出以查看您擁有的 Amazon S3 儲存貯體數量，如果有的話，及其名稱。

1. 選擇新 Amazon S3 儲存貯體的名稱。使用「dotnet-quicktour-s3-1-winvs-」作為基礎，並為其新增唯一項目，例如 GUID 或您的名稱。請務必遵循 [Amazon S3 使用者指南](https://docs.aws.amazon.com/AmazonS3/latest/userguide/)中[儲存貯體命名規則中所述的儲存貯體名稱規則](https://docs.aws.amazon.com/AmazonS3/latest/userguide/BucketRestrictions.html#bucketnamingrules)。

1. 再次執行應用程式，這次提供存儲存貯體名稱。

   在命令列中，使用您選擇的儲存貯體名稱取代下列命令中的 *amzn-s3-demo-bucket*。

   ```
   S3CreateAndList amzn-s3-demo-bucket
   ```

   或者，如果您在 IDE 中執行應用程式，請選擇**專案**、**S3CreateAndList 屬性**、**偵錯**，然後在該處輸入儲存貯體名稱。

1. 檢查輸出以查看建立的新儲存貯體。

## 清除


執行本教學課程時，您建立了一些資源，此時可以選擇清除。
+ 如果您不想保留應用程式在先前步驟中建立的儲存貯體，請使用 Amazon S3 主控台將其刪除，網址為 https：//[https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/)。
+ 如果您不想保留 .NET 專案，請從開發環境中刪除該 `S3CreateAndList` 資料夾。

## 後續作業


返回[快速導覽功能表](quick-start.md)或直接前往[此快速導覽的結尾](quick-start-next-steps.md)。

# 後續步驟


請務必清除您執行這些教學課程時所建立的剩餘資源。這些可能是開發環境中 AWS 的資源，例如檔案和資料夾。

現在您已瀏覽 適用於 .NET 的 AWS SDK，您可能想要[啟動專案](net-dg-start-new-project.md)。