

 AWS SDK for .NET V3 がメンテナンスモードになりました。

[AWS SDK for .NET V4](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/welcome.html) に移行することをお勧めします。移行方法の詳細と情報については、[メンテナンスモードのお知らせ](https://aws.amazon.com/blogs/developer/aws-sdk-for-net-v3-maintenance-mode-announcement/)を参照してください。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# AWS SDK for .NET を使用したシンプルな Windows ベースのアプリケーション
<a name="quick-start-s3-1-winvs"></a>

このチュートリアルでは、Windows 上の AWS SDK for .NET と Visual Studio および .NET Core を使用します。また、SDK を使用して、所有する [ Amazon S3 バケット](https://docs.aws.amazon.com/AmazonS3/latest/userguide/)を一覧表示し、必要に応じてバケットを作成する方法を説明します。

このチュートリアルは、Visual Studio と .NET Core を使用して Windows で実行します。開発環境を設定するその他の方法については、「[ツールチェーンのインストールと設定](net-dg-dev-env.md)」を参照してください。

**Visual Studio および .NET Core を使用した Windows での開発では必須:**
+ [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)しておく必要があります。

## ステップ
<a name="s3-1-winvs-steps"></a>
+ [プロジェクトの作成](#s3-1-winvs-create-project)
+ [コードの作成](#s3-1-winvs-code)
+ [アプリケーションを実行する](#s3-1-winvs-run)
+ [クリーンアップ](#s3-1-winvs-clean-up)

## プロジェクトの作成
<a name="s3-1-winvs-create-project"></a>

1. Visual Studio を開き、C\$1 バージョンの **Console App** テンプレート（説明文に「…for creating a command-line application that can run on .NET…」と記載されているもの）を使用する新しいプロジェクトを作成します。プロジェクトに `S3CreateAndList` という名前を付けます。
**注記**  
.NET Framework バージョンのコンソールアプリケーションテンプレートを選択しないでください。選択する場合は、必ず .NET Framework 4.7.2 以降を使用してください。

1. 新しく作成したプロジェクトを読み込んだら、[**ツール**]、[**NuGet パッケージマネージャー**]、[**ソリューションの NuGet パッケージの管理**] の順に選択します。

1. 次の NuGet パッケージをブラウズし、プロジェクト `AWSSDK.S3`、`AWSSDK.SecurityToken`、`AWSSDK.SSO`、`AWSSDK.SSOOIDC` にインストールします。

   このプロセスでは、[NuGet パッケージ マネージャー](https://www.nuget.org/profiles/awsdotnet)から NuGet パッケージをインストールします。このチュートリアルに必要な NuGet パッケージは正確にわかっているため、ここでこのステップを実行できます。また、開発中に必要なパッケージが判明することは一般的なことです。その場合は、その時点で同様の手順に従ってインストールしてください。

1. コマンドプロンプトからアプリケーションを実行する場合は、ここでコマンドプロンプトを開き、ビルド出力を含むフォルダに移動します。通常は `S3CreateAndList\S3CreateAndList\bin\Debug\net6.0` のようになりますが、環境によって異なります。

## コードの作成
<a name="s3-1-winvs-code"></a>

1. `S3CreateAndList` プロジェクトで、`Program.cs` を探して IDE で開きます。

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 を使用している場合は、次のようなビルドエラーが発生することがあります。  
"Feature 'async main' is not available in C\$1 7.0. Please use language version 7.1 or greater." (C\$1 7.0 では async main 機能を使用できません。言語バージョン 7.1 以降を使用してください。)  
このエラーが発生した場合は、新しいバージョンの言語を使用するようにプロジェクトをセットアップします。これは通常、プロジェクトのプロパティ (**Build**、**Advanced**) で実行します。

## アプリケーションを実行する
<a name="s3-1-winvs-run"></a>

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. 出力を調べて、作成された新しいバケットを確認します。

## クリーンアップ
<a name="s3-1-winvs-clean-up"></a>

このチュートリアルでは、この時点でクリーンアップを選択できるいくつかのリソースを作成しました。
+ 前のステップでアプリケーションが作成したバケットを保持しない場合は、[https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) の Amazon S3 コンソールを使用してバケットを削除します。
+ .NET プロジェクトを保持しない場合は、開発環境から `S3CreateAndList` フォルダを削除します。

## 次の段階
<a name="s3-1-winvs-next"></a>

[クイックツアーメニュー](quick-start.md)に戻るか、[このクイックツアーの最後](quick-start-next-steps.md)までスキップします。