本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
支援 使用 AWS SDKs程式碼範例
下列程式碼範例示範如何 支援 搭配 AWS 軟體開發套件 (SDK) 使用 。
基本概念是程式碼範例,這些範例說明如何在服務內執行基本操作。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。
如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱 搭 AWS 支援 配 AWS SDK 使用。此主題也包含入門相關資訊和舊版 SDK 的詳細資訊。
開始使用
下列程式碼範例示範如何開始使用 支援。
- .NET
-
- AWS SDK for .NET
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 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 詳細資訊,請參閱《AWS SDK for .NET API 參考》中的 DescribeServices。
-
- Java
-
- SDK for Java 2.x
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.support.SupportClient; import software.amazon.awssdk.services.support.model.Category; import software.amazon.awssdk.services.support.model.DescribeServicesRequest; import software.amazon.awssdk.services.support.model.DescribeServicesResponse; import software.amazon.awssdk.services.support.model.Service; import software.amazon.awssdk.services.support.model.SupportException; import java.util.ArrayList; 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 * * 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 詳細資訊,請參閱《AWS SDK for Java 2.x API 參考》中的 DescribeServices。
-
- JavaScript
-
- SDK for JavaScript (v3)
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 呼叫 `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.", ); } 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 詳細資訊,請參閱《AWS SDK for JavaScript API 參考》中的 DescribeServices。
-
- Kotlin
-
- SDK for Kotlin
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /** 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 詳細資訊,請參閱《適用於 Kotlin 的AWS SDK API 參考》中的 DescribeServices
。
-
- Python
-
- SDK for Python (Boto3)
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def hello_support(support_client): """ Use the AWS SDK for Python (Boto3) to create an AWS Support client and count the available services in your account. This example uses the default settings specified in your shared credentials and config files. :param support_client: A Boto3 Support Client object. """ try: print("Hello, AWS Support! Let's count the available Support services:") response = support_client.describe_services() print(f"There are {len(response['services'])} services available.") except ClientError as err: if err.response["Error"]["Code"] == "SubscriptionRequiredException": logger.info( "You must have a Business, Enterprise On-Ramp, or Enterprise Support " "plan to use the AWS Support API. \n\tPlease upgrade your subscription to run these " "examples." ) else: logger.error( "Couldn't count services. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise if __name__ == "__main__": hello_support(boto3.client("support"))
-
如需 API 詳細資訊,請參閱《適用於 Python (Boto3) 的AWS SDK API 參考》中的 DescribeServices。
-
程式碼範例
- 基本概念
- 您好 支援
- 了解基本概念
- 動作
- AddAttachmentsToSet
- AddCommunicationToCase
- CreateCase
- DescribeAttachment
- DescribeCases
- DescribeCommunications
- DescribeServices
- DescribeSeverityLevels
- DescribeTrustedAdvisorCheckRefreshStatuses
- DescribeTrustedAdvisorCheckResult
- DescribeTrustedAdvisorCheckSummaries
- DescribeTrustedAdvisorChecks
- RefreshTrustedAdvisorCheck
- ResolveCase