文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 AWS SDKs Amazon ECS 程式碼範例
下列程式碼範例示範如何使用 Amazon Elastic Container Service 搭配 AWS 軟體開發套件 (SDK)。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。
案例是向您展示如何呼叫服務中的多個函數或與其他 AWS 服務組合來完成特定任務的程式碼範例。
開始使用
下列程式碼範例說明如何開始使用 Amazon ECS。
- .NET
-
- AWS SDK for .NET
-
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();
}
}
}