将 ListClusters 与 AWS SDK 或 CLI 配合使用 - AWS SDK 代码示例

AWS 文档 SDK 示例 GitHub 存储库中还有更多 AWS SDK 示例。

ListClusters 与 AWS SDK 或 CLI 配合使用

以下代码示例演示如何使用 ListClusters

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
SDK for .NET
注意

查看 GitHub,了解更多信息。在 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}"); } }
  • 有关 API 详细信息,请参阅《AWS SDK for .NET API Reference》中的 ListClusters

CLI
AWS CLI

列出您的可用集群

以下 list-clusters 示例将列出所有可用的集群。

aws ecs list-clusters

输出:

{ "clusterArns": [ "arn:aws:ecs:us-west-2:123456789012:cluster/MyECSCluster1", "arn:aws:ecs:us-west-2:123456789012:cluster/AnotherECSCluster" ] }

有关更多信息,请参阅《Amazon ECS 开发人员指南》中的 Amazon ECS 集群

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ListClusters

Java
适用于 Java 的 SDK 2.x
注意

查看 GitHub,了解更多信息。在 AWS 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.ListClustersResponse; import software.amazon.awssdk.services.ecs.model.EcsException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListClusters { public static void main(String[] args) { Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); listAllClusters(ecsClient); ecsClient.close(); } public static void listAllClusters(EcsClient ecsClient) { try { ListClustersResponse response = ecsClient.listClusters(); List<String> clusters = response.clusterArns(); for (String cluster : clusters) { System.out.println("The cluster arn is " + cluster); } } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 有关 API 的详细信息,请参阅《AWS SDK for Java 2.x API Reference》中的 ListClusters

PowerShell
Tools for PowerShell V4

示例 1:此 cmdlet 返回现有 ECS 集群的列表。

Get-ECSClusterList

输出

arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS-CL arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS
  • 有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》中的 ListClusters

Tools for PowerShell V5

示例 1:此 cmdlet 返回现有 ECS 集群的列表。

Get-ECSClusterList

输出

arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS-CL arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS
  • 有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet Reference (V5)》中的 ListClusters