使用 的簡易 Windows 型應用程式 AWS SDK for .NET - AWS SDK for .NET

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

使用 的簡易 Windows 型應用程式 AWS SDK for .NET

本教學課程使用 Windows AWS SDK for .NET 上的 搭配 Visual Studio 和 。NET 核心。本教學課程說明如何使用 SDK列出您擁有的 Amazon S3 儲存貯體,並選擇性地建立儲存貯體。

您將使用 Visual Studio 和 在 Windows 上執行本教學課程。NET 核心。如需設定開發環境的其他方法,請參閱 安裝和設定您的工具鏈

在 Windows 上使用 Visual Studio 和 進行開發時需要。NET 核心:

  • Microsoft 。NET Core 2.1、3.1 或更新版本

    在安裝最新版本的 Visual Studio 時,通常預設包含此項目。

注意

使用這些教學課程之前,您必須先安裝工具鏈設定SDK身分驗證

步驟

建立專案

  1. 開啟 Visual Studio 並建立新的專案,該專案使用主控台應用程式範本的 C# 版本;亦即,使用描述:「...用於建立可在 .NET...」上執行的命令列應用程式。命名專案 S3CreateAndList

    注意

    請勿選擇 。NET 主控台應用程式範本的架構版本,或者,如果您這麼做,請務必使用 。NET Framework 4.7.2 或更新版本。

  2. 載入新建立的專案後,選擇 Tools NuGet Package Manager Manage NuGet Packages for Solution

  3. 瀏覽下列 NuGet 套件,並將其安裝到專案中:AWSSDK.S3AWSSDK.SSOAWSSDK.SecurityTokenAWSSDK.SSOOIDC

    此程序會從 NuGet 套件NuGet 管理員 安裝套件。由於我們確切知道本教學課程所需的 NuGet 套件,因此現在可以執行此步驟。開發期間,通常也會知道所需的套件。發生這種情況時,請按照類似的過程進行安裝。

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

建立程式碼

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

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

    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; } } }
  3. 建置應用程式。

    注意

    如果您使用的是較舊版本的 Visual Studio,則可能會出現類似下列的建置錯誤:

    C# 7.0 中無法使用「功能「非同步主體」。請使用語言版本 7.1 或更新版本。"

    如果您收到此錯誤,請將專案設定為使用較新版本的語言。這通常在專案屬性、建置 進階 中完成。

執行應用程式

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

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

  3. 選擇新 Amazon S3 儲存貯體的名稱。使用 "dotnet-quicktour-s3-1-winvs-" 作為基礎,並為其新增唯一內容,例如 GUID或您的名稱。請務必遵循儲存貯體名稱的規則,如 Amazon S3 使用者指南 中的儲存貯體命名規則所述。

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

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

    S3CreateAndList amzn-s3-demo-bucket

    或者,如果您在 中執行應用程式IDE,請選擇 Project S3CreateAndList Properties Debug 並在其中輸入儲存貯體名稱。

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

清除

執行本教學課程時,您建立了一些資源,此時可以選擇清除。

  • 如果您不想保留應用程式在先前步驟中建立的儲存貯體,請使用位於 的 Amazon S3 主控台將其刪除https://console.aws.amazon.com/s3/

  • 如果您不想保留 .NET 專案,請從開發環境中移除 S3CreateAndList 資料夾。

後續作業

返回快速導覽選單,或直接前往此快速導覽的結尾。