将 ListTargetsByRule 与 AWS SDK 或 CLI 配合使用
以下代码示例演示如何使用 ListTargetsByRule。
操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:
- .NET
-
- 适用于 .NET 的 SDK
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 使用规则名称列出规则的所有目标。
/// <summary> /// List all of the targets matching a rule by name. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <returns>The list of targets.</returns> public async Task<List<Target>> ListAllTargetsOnRule(string ruleName) { var results = new List<Target>(); var request = new ListTargetsByRuleRequest() { Rule = ruleName }; ListTargetsByRuleResponse response; do { response = await _amazonEventBridge.ListTargetsByRuleAsync(request); results.AddRange(response.Targets); request.NextToken = response.NextToken; } while (response.NextToken is not null); return results; }-
有关 API 详细信息,请参阅《适用于 .NET 的 AWS SDK API 参考》中的 ListTargetsByRule。
-
- CLI
-
- AWS CLI
-
显示 CloudWatch Events 规则的所有目标
以下示例显示了名为 DailyLambdaFunction 的规则的所有目标:
aws events list-targets-by-rule --rule"DailyLambdaFunction"-
有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ListTargetsByRule
。
-
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 使用规则名称列出规则的所有目标。
public static void listTargets(EventBridgeClient eventBrClient, String ruleName) { ListTargetsByRuleRequest ruleRequest = ListTargetsByRuleRequest.builder() .rule(ruleName) .build(); ListTargetsByRuleResponse res = eventBrClient.listTargetsByRule(ruleRequest); List<Target> targetsList = res.targets(); for (Target target: targetsList) { System.out.println("Target ARN: "+target.arn()); } }-
有关 API 详细信息,请参阅《AWS SDK for Java 2.x API 参考》中的 ListTargetsByRule。
-
- Kotlin
-
- SDK for Kotlin
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 suspend fun listTargets(ruleName: String?) { val ruleRequest = ListTargetsByRuleRequest { rule = ruleName } EventBridgeClient.fromEnvironment { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listTargetsByRule(ruleRequest) response.targets?.forEach { target -> println("Target ARN: ${target.arn}") } } }-
有关 API 详细信息,请参阅《AWS SDK for Kotlin API 参考》中的 ListTargetsByRule
。
-
有关 AWS SDK 开发人员指南和代码示例的完整列表,请参阅 将 EventBridge 与 AWS SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。
ListRules
PutEvents