ECSContoh Amazon menggunakan AWS SDK for .NET - AWS SDK for .NET

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

ECSContoh Amazon menggunakan AWS SDK for .NET

Contoh kode berikut menunjukkan kepada Anda cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan AWS SDK for .NET With AmazonECS.

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.

Skenario adalah contoh kode yang menunjukkan kepada Anda bagaimana menyelesaikan tugas tertentu dengan memanggil beberapa fungsi dalam layanan atau dikombinasikan dengan yang lain Layanan AWS.

Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.

Memulai

Contoh kode berikut menunjukkan cara memulai menggunakan AmazonECS.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

using Amazon.ECS; using Amazon.ECS.Model; using Microsoft.Extensions.Hosting; namespace ECSActions; public class HelloECS { static async System.Threading.Tasks.Task Main(string[] args) { // Use the AWS .NET Core Setup package to set up dependency injection for the Amazon ECS domain registration service. // Use your AWS profile name, or leave it blank to use the default profile. using var host = Host.CreateDefaultBuilder(args).Build(); // Now the client is available for injection. var amazonECSClient = new AmazonECSClient(); // You can use await and any of the async methods to get a response. var response = await amazonECSClient.ListClustersAsync(new ListClustersRequest { }); Console.WriteLine($"Hello Amazon ECS! Following are some cluster ARNS available in the your aws account"); Console.WriteLine(); foreach (var arn in response.ClusterArns.Take(5)) { Console.WriteLine($"\tARN: {arn}"); Console.WriteLine($"Cluster Name: {arn.Split("/").Last()}"); Console.WriteLine(); } } }
  • Untuk API detailnya, lihat ListClustersdi AWS SDK for .NET APIReferensi.

Tindakan

Contoh kode berikut menunjukkan cara menggunakanListClusters.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// List cluster ARNs available. /// </summary> /// <returns>The ARN list of clusters.</returns> public async Task<List<string>> GetClusterARNSAsync() { Console.WriteLine("Getting a list of all the clusters in your AWS account..."); List<string> clusterArnList = new List<string>(); // Get a list of all the clusters in your AWS account try { var listClustersResponse = _ecsClient.Paginators.ListClusters(new ListClustersRequest { }); var clusterArns = listClustersResponse.ClusterArns; // Print the ARNs of the clusters await foreach (var clusterArn in clusterArns) { clusterArnList.Add(clusterArn); } if (clusterArnList.Count == 0) { _logger.LogWarning("No clusters found in your AWS account."); } return clusterArnList; } catch (Exception e) { _logger.LogError($"An error occurred while getting a list of all the clusters in your AWS account. {e.InnerException}"); throw new Exception($"An error occurred while getting a list of all the clusters in your AWS account. {e.InnerException}"); } }
  • Untuk API detailnya, lihat ListClustersdi AWS SDK for .NET APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanListServices.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// List service ARNs available. /// </summary> /// <param name="clusterARN">The arn of the ECS cluster.</param> /// <returns>The ARN list of services in given cluster.</returns> public async Task<List<string>> GetServiceARNSAsync(string clusterARN) { List<string> serviceArns = new List<string>(); var request = new ListServicesRequest { Cluster = clusterARN }; // Call the ListServices API operation and get the list of service ARNs var serviceList = _ecsClient.Paginators.ListServices(request); await foreach (var serviceARN in serviceList.ServiceArns) { if (serviceARN is null) continue; serviceArns.Add(serviceARN); } if (serviceArns.Count == 0) { _logger.LogWarning($"No services found in cluster {clusterARN} ."); } return serviceArns; }
  • Untuk API detailnya, lihat ListServicesdi AWS SDK for .NET APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanListTasks.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

/// <summary> /// List task ARNs available. /// </summary> /// <param name="clusterARN">The arn of the ECS cluster.</param> /// <returns>The ARN list of tasks in given cluster.</returns> public async Task<List<string>> GetTaskARNsAsync(string clusterARN) { // Set up the request to describe the tasks in the service var listTasksRequest = new ListTasksRequest { Cluster = clusterARN }; List<string> taskArns = new List<string>(); // Call the ListTasks API operation and get the list of task ARNs var tasks = _ecsClient.Paginators.ListTasks(listTasksRequest); await foreach (var task in tasks.TaskArns) { if (task is null) continue; taskArns.Add(task); } if (taskArns.Count == 0) { _logger.LogWarning("No tasks found in cluster: " + clusterARN); } return taskArns; }
  • Untuk API detailnya, lihat ListTasksdi AWS SDK for .NET APIReferensi.

Skenario

Contoh kode berikut ini menunjukkan cara:

  • Dapatkan daftar semua cluster.

  • Dapatkan layanan untuk cluster.

  • Dapatkan tugas untuk cluster.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Jalankan skenario interaktif di penggugah/prompt perintah.

using Amazon.ECS; using ECSActions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Console; using Microsoft.Extensions.Logging.Debug; namespace ECSScenario; public class ECSScenario { /* Before running this .NET code example, set up your development environment, including your credentials. This .NET example performs the following tasks: 1. List ECS Cluster ARNs. 2. List services in every cluster 3. List Task ARNs in every cluster. */ private static ILogger logger = null!; private static ECSWrapper _ecsWrapper = null!; static async Task Main(string[] args) { // Set up dependency injection for the Amazon service. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .Build(); ILoggerFactory loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<ECSScenario>(); var loggerECSWarpper = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<ECSWrapper>(); var amazonECSClient = new AmazonECSClient(); _ecsWrapper = new ECSWrapper(amazonECSClient, loggerECSWarpper); Console.WriteLine(new string('-', 80)); Console.WriteLine("Welcome to the Amazon ECS example scenario."); Console.WriteLine(new string('-', 80)); try { await ListClusterARNs(); await ListServiceARNs(); await ListTaskARNs(); } catch (Exception ex) { logger.LogError(ex, "There was a problem executing the scenario."); } } /// <summary> /// List ECS Cluster ARNs /// </summary> private static async Task ListClusterARNs() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"1. List Cluster ARNs from ECS."); var arns = await _ecsWrapper.GetClusterARNSAsync(); foreach (var arn in arns) { Console.WriteLine($"Cluster arn: {arn}"); Console.WriteLine($"Cluster name: {arn.Split("/").Last()}"); } Console.WriteLine(new string('-', 80)); } /// <summary> /// List services in every cluster /// </summary> private static async Task ListServiceARNs() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"2. List Service ARNs in every cluster."); var clusterARNs = await _ecsWrapper.GetClusterARNSAsync(); foreach (var clusterARN in clusterARNs) { Console.WriteLine($"Getting services for cluster name: {clusterARN.Split("/").Last()}"); Console.WriteLine(new string('.', 5)); var serviceARNs = await _ecsWrapper.GetServiceARNSAsync(clusterARN); foreach (var serviceARN in serviceARNs) { Console.WriteLine($"Service arn: {serviceARN}"); Console.WriteLine($"Service name: {serviceARN.Split("/").Last()}"); } } Console.WriteLine(new string('-', 80)); } /// <summary> /// List tasks in every cluster /// </summary> private static async Task ListTaskARNs() { Console.WriteLine(new string('-', 80)); Console.WriteLine($"3. List Task ARNs in every cluster."); var clusterARNs = await _ecsWrapper.GetClusterARNSAsync(); foreach (var clusterARN in clusterARNs) { Console.WriteLine($"Getting tasks for cluster name: {clusterARN.Split("/").Last()}"); Console.WriteLine(new string('.', 5)); var taskARNs = await _ecsWrapper.GetTaskARNsAsync(clusterARN); foreach (var taskARN in taskARNs) { Console.WriteLine($"Task arn: {taskARN}"); } } Console.WriteLine(new string('-', 80)); } }

Metode pembungkus yang dipanggil oleh skenario untuk mengelola ECS tindakan Amazon.

using Amazon.ECS; using Amazon.ECS.Model; using Microsoft.Extensions.Logging; namespace ECSActions; public class ECSWrapper { private readonly AmazonECSClient _ecsClient; private readonly ILogger<ECSWrapper> _logger; /// <summary> /// Constructor for the ECS wrapper. /// </summary> /// <param name="ecsClient">The injected ECS client.</param> /// <param name="logger">The injected logger for the wrapper.</param> public ECSWrapper(AmazonECSClient ecsClient, ILogger<ECSWrapper> logger) { _logger = logger; _ecsClient = ecsClient; } /// <summary> /// List cluster ARNs available. /// </summary> /// <returns>The ARN list of clusters.</returns> public async Task<List<string>> GetClusterARNSAsync() { Console.WriteLine("Getting a list of all the clusters in your AWS account..."); List<string> clusterArnList = new List<string>(); // Get a list of all the clusters in your AWS account try { var listClustersResponse = _ecsClient.Paginators.ListClusters(new ListClustersRequest { }); var clusterArns = listClustersResponse.ClusterArns; // Print the ARNs of the clusters await foreach (var clusterArn in clusterArns) { clusterArnList.Add(clusterArn); } if (clusterArnList.Count == 0) { _logger.LogWarning("No clusters found in your AWS account."); } return clusterArnList; } catch (Exception e) { _logger.LogError($"An error occurred while getting a list of all the clusters in your AWS account. {e.InnerException}"); throw new Exception($"An error occurred while getting a list of all the clusters in your AWS account. {e.InnerException}"); } } /// <summary> /// List service ARNs available. /// </summary> /// <param name="clusterARN">The arn of the ECS cluster.</param> /// <returns>The ARN list of services in given cluster.</returns> public async Task<List<string>> GetServiceARNSAsync(string clusterARN) { List<string> serviceArns = new List<string>(); var request = new ListServicesRequest { Cluster = clusterARN }; // Call the ListServices API operation and get the list of service ARNs var serviceList = _ecsClient.Paginators.ListServices(request); await foreach (var serviceARN in serviceList.ServiceArns) { if (serviceARN is null) continue; serviceArns.Add(serviceARN); } if (serviceArns.Count == 0) { _logger.LogWarning($"No services found in cluster {clusterARN} ."); } return serviceArns; } /// <summary> /// List task ARNs available. /// </summary> /// <param name="clusterARN">The arn of the ECS cluster.</param> /// <returns>The ARN list of tasks in given cluster.</returns> public async Task<List<string>> GetTaskARNsAsync(string clusterARN) { // Set up the request to describe the tasks in the service var listTasksRequest = new ListTasksRequest { Cluster = clusterARN }; List<string> taskArns = new List<string>(); // Call the ListTasks API operation and get the list of task ARNs var tasks = _ecsClient.Paginators.ListTasks(listTasksRequest); await foreach (var task in tasks.TaskArns) { if (task is null) continue; taskArns.Add(task); } if (taskArns.Count == 0) { _logger.LogWarning("No tasks found in cluster: " + clusterARN); } return taskArns; } }