AWS文件 AWS SDK 範例 GitHub 存放庫中提供了更多 SDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 AWS SDK 的 Step Functions 的程式碼範例
下列程式碼範例會示範如何搭AWS Step Functions配AWS軟體開發套件 (SDK) 使用。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境和跨服務範例中查看內容中的動作。
案例是向您展示如何呼叫相同服務中的多個函數來完成特定任務的程式碼範例。
Cross-service examples (跨服務範例) 是跨多個 AWS 服務 執行的應用程式範例。
其他 資源
Step Functions 開發人員指南-有關 Step Functions 的更多信息。
Step Functions API 參考 — 有關所有可用 Step Functions 動作的詳細資訊。
AWS開發人員中心
— 您可以依類別或全文檢索搜尋篩選的程式碼範例。 AWSSDK 示例
— 具有首選語言的完整代碼的 GitHub 回購。包括設定和執行程式碼的指示。
開始使用
下列程式碼範例會示範如何開始使用 Step Functions 式。
- .NET
-
- AWS SDK for .NET
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 namespace StepFunctionsActions; using Amazon.StepFunctions; using Amazon.StepFunctions.Model; public class HelloStepFunctions { static async Task Main() { var stepFunctionsClient = new AmazonStepFunctionsClient(); Console.Clear(); Console.WriteLine("Welcome to AWS Step Functions"); Console.WriteLine("Let's list up to 10 of your state machines:"); var stateMachineListRequest = new ListStateMachinesRequest { MaxResults = 10 }; // Get information for up to 10 Step Functions state machines. var response = await stepFunctionsClient.ListStateMachinesAsync(stateMachineListRequest); if (response.StateMachines.Count > 0) { response.StateMachines.ForEach(stateMachine => { Console.WriteLine($"State Machine Name: {stateMachine.Name}\tAmazon Resource Name (ARN): {stateMachine.StateMachineArn}"); }); } else { Console.WriteLine("\tNo state machines were found."); } } }
-
如需 API 詳細資訊,請參閱 AWS SDK for .NETAPI 參考ListStateMachines中的。
-
- Java
-
- 適用於 Java 2.x 的 SDK
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 你好的 Java 版本。
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sfn.SfnClient; import software.amazon.awssdk.services.sfn.model.ListStateMachinesResponse; import software.amazon.awssdk.services.sfn.model.SfnException; import software.amazon.awssdk.services.sfn.model.StateMachineListItem; 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 ListStateMachines { public static void main(String[] args) { Region region = Region.US_EAST_1; SfnClient sfnClient = SfnClient.builder() .region(region) .build(); listMachines(sfnClient); sfnClient.close(); } public static void listMachines(SfnClient sfnClient) { try { ListStateMachinesResponse response = sfnClient.listStateMachines(); List<StateMachineListItem> machines = response.stateMachines(); for (StateMachineListItem machine : machines) { System.out.println("The name of the state machine is: " + machine.name()); System.out.println("The ARN value is : " + machine.stateMachineArn()); } } catch (SfnException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
-
如需 API 詳細資訊,請參閱 AWS SDK for Java 2.xAPI 參考ListStateMachines中的。
-
- Kotlin
-
- 適用於 Kotlin 的 SDK
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 import aws.sdk.kotlin.services.sfn.SfnClient import aws.sdk.kotlin.services.sfn.model.ListStateMachinesRequest /** 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 */ suspend fun main() { println(DASHES) println("Welcome to the AWS Step Functions Hello example.") println("Lets list up to ten of your state machines:") println(DASHES) listMachines() } suspend fun listMachines() { SfnClient { region = "us-east-1" }.use { sfnClient -> val response = sfnClient.listStateMachines(ListStateMachinesRequest {}) response.stateMachines?.forEach { machine -> println("The name of the state machine is ${machine.name}") println("The ARN value is ${machine.stateMachineArn}") } } }
-
有關 API 的詳細信息,請參閱 AWSSDK ListStateMachines
中的 Kotlin API 參考。
-
- Python
-
- 適用於 Python (Boto3) 的 SDK
-
注意
還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫
。 import boto3 def hello_stepfunctions(stepfunctions_client): """ Use the AWS SDK for Python (Boto3) to create an AWS Step Functions client and list the state machines in your account. This list might be empty if you haven't created any state machines. This example uses the default settings specified in your shared credentials and config files. :param stepfunctions_client: A Boto3 Step Functions Client object. """ print("Hello, Step Functions! Let's list up to 10 of your state machines:") state_machines = stepfunctions_client.list_state_machines(maxResults=10) for sm in state_machines["stateMachines"]: print(f"\t{sm['name']}: {sm['stateMachineArn']}") if __name__ == "__main__": hello_stepfunctions(boto3.client("stepfunctions"))
-
如需 API 的詳細資訊,請參閱AWS開發套件ListStateMachines中的 Python (博托 3) API 參考。
-