使用 Amazon 开发工具包的 Amazon Web Services Support 代码示例
以下代码示例显示如何将 Amazon Web Services Support 与 Amazon 软件开发工具包(SDK)一起使用。
操作是展示如何调用具体服务函数的代码节选。
场景是展示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。
有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 Amazon Web Services Support 与 Amazon 开发工具包配合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。
开始使用
以下代码示例显示如何开始使用 Amazon Web Services Support。
- .NET
-
- Amazon SDK for .NET
-
注意
在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 using Amazon.AWSSupport; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; public static class HelloSupport { static async Task Main(string[] args) { // Use the AWS .NET Core Setup package to set up dependency injection for the AWS Support service. // Use your AWS profile name, or leave it blank to use the default profile. // You must have one of the following AWS Support plans: Business, Enterprise On-Ramp, or Enterprise. Otherwise, an exception will be thrown. using var host = Host.CreateDefaultBuilder(args) .ConfigureServices((_, services) => services.AddAWSService<IAmazonAWSSupport>() ).Build(); // Now the client is available for injection. var supportClient = host.Services.GetRequiredService<IAmazonAWSSupport>(); // You can use await and any of the async methods to get a response. var response = await supportClient.DescribeServicesAsync(); Console.WriteLine($"\tHello AWS Support! There are {response.Services.Count} services available."); } }
-
有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 DescribeServices。
-
- Java
-
- SDK for Java 2.x
-
注意
在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /** * 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 * * In addition, you must have the AWS Business Support Plan to use the AWS Support Java API. For more information, see: * * https://aws.amazon.com/premiumsupport/plans/ * * This Java example performs the following task: * * 1. Gets and displays available services. * * * NOTE: To see multiple operations, see SupportScenario. */ public class HelloSupport { public static void main(String[] args) { Region region = Region.US_WEST_2; SupportClient supportClient = SupportClient.builder() .region(region) .build(); System.out.println("***** Step 1. Get and display available services."); displayServices(supportClient); } // Return a List that contains a Service name and Category name. public static void displayServices(SupportClient supportClient) { try { DescribeServicesRequest servicesRequest = DescribeServicesRequest.builder() .language("en") .build(); DescribeServicesResponse response = supportClient.describeServices(servicesRequest); List<Service> services = response.services(); System.out.println("Get the first 10 services"); int index = 1; for (Service service: services) { if (index== 11) break; System.out.println("The Service name is: "+service.name()); // Display the Categories for this service. List<Category> categories = service.categories(); for (Category cat: categories) { System.out.println("The category name is: "+cat.name()); } index++ ; } } catch (SupportException e) { System.out.println(e.getLocalizedMessage()); System.exit(1); } } }
-
有关 API 详细信息,请参阅《Amazon SDK for Java 2.x API 参考》中的 DescribeServices。
-
- JavaScript
-
- SDK for JavaScript V3
-
注意
在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 调用 `main()` 运行该示例。
import { DescribeServicesCommand, SupportClient, } from "@aws-sdk/client-support"; // Change the value of 'region' to your preferred AWS Region. const client = new SupportClient({ region: "us-east-1" }); const getServiceCount = async () => { try { const { services } = await client.send(new DescribeServicesCommand({})); return services.length; } catch (err) { if (err.name === "SubscriptionRequiredException") { throw new Error( "You must be subscribed to the AWS Support plan to use this feature." ); } else { throw err; } } }; export const main = async () => { try { const count = await getServiceCount(); console.log(`Hello, AWS Support! There are ${count} services available.`); } catch (err) { console.error("Failed to get service count: ", err.message); } };
-
有关 API 详细信息,请参阅《Amazon SDK for JavaScript API 参考》中的 DescribeServices。
-
- Kotlin
-
- SDK for Kotlin
-
注意
这是适用于预览版中功能的预发行文档。本文档随时可能更改。
注意
在 GitHub 上查看更多内容。在 Amazon 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /** Before running this Kotlin 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-kotlin/latest/developer-guide/setup.html In addition, you must have the AWS Business Support Plan to use the AWS Support Java API. For more information, see: https://aws.amazon.com/premiumsupport/plans/ This Kotlin example performs the following task: 1. Gets and displays available services. */ suspend fun main() { displaySomeServices() } // Return a List that contains a Service name and Category name. suspend fun displaySomeServices() { val servicesRequest = DescribeServicesRequest { language = "en" } SupportClient { region = "us-west-2" }.use { supportClient -> val response = supportClient.describeServices(servicesRequest) println("Get the first 10 services") var index = 1 response.services?.forEach { service -> if (index == 11) { return@forEach } println("The Service name is: " + service.name) // Get the categories for this service. service.categories?.forEach { cat -> println("The category name is ${cat.name}") index++ } } } }
-
有关 API 详细信息,请参阅《Amazon SDK for Kotlin API 参考》中的 DescribeServices
。
-