RemoveTargets 搭配 AWS SDK或 使用 CLI - Amazon EventBridge

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

RemoveTargets 搭配 AWS SDK或 使用 CLI

下列程式碼範例示範如何使用 RemoveTargets

.NET
AWS SDK for .NET
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

使用規則名稱移除規則的所有目標。

/// <summary> /// Delete an event rule by name. /// </summary> /// <param name="ruleName">The name of the event rule.</param> /// <returns>True if successful.</returns> public async Task<bool> RemoveAllTargetsFromRule(string ruleName) { var targetIds = new List<string>(); var request = new ListTargetsByRuleRequest() { Rule = ruleName }; ListTargetsByRuleResponse targetsResponse; do { targetsResponse = await _amazonEventBridge.ListTargetsByRuleAsync(request); targetIds.AddRange(targetsResponse.Targets.Select(t => t.Id)); request.NextToken = targetsResponse.NextToken; } while (targetsResponse.NextToken is not null); var removeResponse = await _amazonEventBridge.RemoveTargetsAsync( new RemoveTargetsRequest() { Rule = ruleName, Ids = targetIds }); if (removeResponse.FailedEntryCount > 0) { removeResponse.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to remove target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return removeResponse.HttpStatusCode == HttpStatusCode.OK; }
  • 如需API詳細資訊,請參閱 參考 RemoveTargets中的 。 AWS SDK for .NET API

CLI
AWS CLI

移除事件的目標

此範例會將名為 MyStream1 的 Amazon Kinesis 串流從規則 的目標中移除 DailyLambdaFunction。 DailyLambdaFunction 建立 時,此串流設定為 ID 為 Target1 的目標:

aws events remove-targets --rule "DailyLambdaFunction" --ids "Target1"
  • 如需API詳細資訊,請參閱 命令參考 RemoveTargets中的 。 AWS CLI

Java
SDK 適用於 Java 2.x
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

使用規則名稱移除規則的所有目標。

public static void deleteTargetsFromRule(EventBridgeClient eventBrClient, String eventRuleName) { // First, get all targets that will be deleted. ListTargetsByRuleRequest request = ListTargetsByRuleRequest.builder() .rule(eventRuleName) .build(); ListTargetsByRuleResponse response = eventBrClient.listTargetsByRule(request); List<Target> allTargets = response.targets(); // Get all targets and delete them. for (Target myTarget : allTargets) { RemoveTargetsRequest removeTargetsRequest = RemoveTargetsRequest.builder() .rule(eventRuleName) .ids(myTarget.id()) .build(); eventBrClient.removeTargets(removeTargetsRequest); System.out.println("Successfully removed the target"); } }
  • 如需API詳細資訊,請參閱 參考 RemoveTargets中的 。 AWS SDK for Java 2.x API

Kotlin
SDK 適用於 Kotlin
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun deleteTargetsFromRule(eventRuleName: String?) { // First, get all targets that will be deleted. val request = ListTargetsByRuleRequest { rule = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listTargetsByRule(request) val allTargets = response.targets // Get all targets and delete them. if (allTargets != null) { for (myTarget in allTargets) { val removeTargetsRequest = RemoveTargetsRequest { rule = eventRuleName ids = listOf(myTarget.id.toString()) } eventBrClient.removeTargets(removeTargetsRequest) println("Successfully removed the target") } } } }
  • 如需API詳細資訊,請參閱RemoveTargets中的 AWS SDK for Kotlin API參考

如需開發人員指南和程式碼範例的完整清單 AWS SDK,請參閱 EventBridge 搭配 使用 AWS SDK。本主題也包含有關入門的資訊,以及先前SDK版本的詳細資訊。