

のバージョン 4 (V4) AWS SDK for .NET がリリースされました。

重要な変更とアプリケーションの移行については、[「移行トピック](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)

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

# を使用したシンプルなクロスプラットフォームアプリケーション AWS SDK for .NET
<a name="quick-start-s3-1-cross"></a>

このチュートリアルでは、クロスプラットフォーム開発に AWS SDK for .NET と .NET Core を使用します。また、SDK を使用して、所有する [ Amazon S3 バケット](https://docs.aws.amazon.com/AmazonS3/latest/userguide/)を一覧表示し、必要に応じてバケットを作成する方法を説明します。

このチュートリアルは、.NET コマンドラインインターフェイス (CLI) などのクロスプラットフォームツールを使用して実行します。開発環境を設定するその他の方法については、「[用のツールチェーンのインストールと設定 AWS SDK for .NET](net-dg-dev-env.md)」を参照してください。

**Windows、Linux、または macOS でのクロスプラットフォームの .NET 開発では必須:**
+ Microsoft [.NET Core SDK](https://learn.microsoft.com/en-us/dotnet/fundamentals/)、バージョン 2.1、3.1 以降。.NET コマンドラインインターフェイス (CLI) (**`dotnet`**) および .NET Core ランタイムを含むものとします。
+ 使用しているオペレーティングシステムと要件に適したコードエディタまたは統合開発環境 (IDE)。これは通常、.NET Core のサポートを含むものです。

  例として、[Microsoft Visual Studio Code (VS Code)](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)必要があります。

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

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

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

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

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;
           }
       }
   }
   ```

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

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

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

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

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

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