Ci sono altri AWS SDK esempi disponibili nel repository AWS Doc SDK Examples
Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
EventBridge esempi di utilizzo di for Kotlin SDK
I seguenti esempi di codice mostrano come eseguire azioni e implementare scenari comuni utilizzando AWS SDK for Kotlin with. EventBridge
Le basi sono esempi di codice che mostrano come eseguire le operazioni essenziali all'interno di un servizio.
Le operazioni sono estratti di codice da programmi più grandi e devono essere eseguite nel contesto. Mentre le azioni mostrano come richiamare le singole funzioni di servizio, è possibile visualizzare le azioni nel loro contesto nei relativi scenari.
Ogni esempio include un collegamento al codice sorgente completo, in cui è possibile trovare istruzioni su come configurare ed eseguire il codice nel contesto.
Nozioni di base
I seguenti esempi di codice mostrano come iniziare a utilizzare EventBridge.
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. import aws.sdk.kotlin.services.eventbridge.EventBridgeClient import aws.sdk.kotlin.services.eventbridge.model.ListEventBusesRequest import aws.sdk.kotlin.services.eventbridge.model.ListEventBusesResponse suspend fun main() { listBusesHello() } suspend fun listBusesHello() { val request = ListEventBusesRequest { limit = 10 } EventBridgeClient { region = "us-west-2" }.use { eventBrClient -> val response: ListEventBusesResponse = eventBrClient.listEventBuses(request) response.eventBuses?.forEach { bus -> println("The name of the event bus is ${bus.name}") println("The ARN of the event bus is ${bus.arn}") } } }
-
Per API i dettagli, vedi il riferimento ListEventBuses AWS
SDKa Kotlin API.
-
Argomenti
Nozioni di base
L'esempio di codice seguente mostra come:
Creare una regola e aggiungervi una destinazione.
Abilitare e disabilitare regole.
Elencare e aggiornare regole e destinazioni.
Inviare eventi e quindi eliminare le risorse.
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice 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 This Kotlin example performs the following tasks with Amazon EventBridge: 1. Creates an AWS Identity and Access Management (IAM) role to use with Amazon EventBridge. 2. Creates an Amazon Simple Storage Service (Amazon S3) bucket with EventBridge events enabled. 3. Creates a rule that triggers when an object is uploaded to Amazon S3. 4. Lists rules on the event bus. 5. Creates a new Amazon Simple Notification Service (Amazon SNS) topic and lets the user subscribe to it. 6. Adds a target to the rule that sends an email to the specified topic. 7. Creates an EventBridge event that sends an email when an Amazon S3 object is created. 8. Lists targets. 9. Lists the rules for the same target. 10. Triggers the rule by uploading a file to the S3 bucket. 11. Disables a specific rule. 12. Checks and prints the state of the rule. 13. Adds a transform to the rule to change the text of the email. 14. Enables a specific rule. 15. Triggers the updated rule by uploading a file to the S3 bucket. 16. Updates the rule to a custom rule pattern. 17. Sends an event to trigger the rule. 18. Cleans up resources. */ val DASHES: String = String(CharArray(80)).replace("\u0000", "-") suspend fun main(args: Array<String>) { val usage = """ Usage: <roleName> <bucketName> <topicName> <eventRuleName> Where: roleName - The name of the role to create. bucketName - The Amazon Simple Storage Service (Amazon S3) bucket name to create. topicName - The name of the Amazon Simple Notification Service (Amazon SNS) topic to create. eventRuleName - The Amazon EventBridge rule name to create. """ val polJSON = "{" + "\"Version\": \"2012-10-17\"," + "\"Statement\": [{" + "\"Effect\": \"Allow\"," + "\"Principal\": {" + "\"Service\": \"events.amazonaws.com\"" + "}," + "\"Action\": \"sts:AssumeRole\"" + "}]" + "}" if (args.size != 4) { println(usage) exitProcess(1) } val sc = Scanner(System.`in`) val roleName = args[0] val bucketName = args[1] val topicName = args[2] val eventRuleName = args[3] println(DASHES) println("Welcome to the Amazon EventBridge example scenario.") println(DASHES) println(DASHES) println("1. Create an AWS Identity and Access Management (IAM) role to use with Amazon EventBridge.") val roleArn = createIAMRole(roleName, polJSON) println(DASHES) println(DASHES) println("2. Create an S3 bucket with EventBridge events enabled.") if (checkBucket(bucketName)) { println("$bucketName already exists. Ending this scenario.") exitProcess(1) } createBucket(bucketName) delay(3000) setBucketNotification(bucketName) println(DASHES) println(DASHES) println("3. Create a rule that triggers when an object is uploaded to Amazon S3.") delay(10000) addEventRule(roleArn, bucketName, eventRuleName) println(DASHES) println(DASHES) println("4. List rules on the event bus.") listRules() println(DASHES) println(DASHES) println("5. Create a new SNS topic for testing and let the user subscribe to the topic.") val topicArn = createSnsTopic(topicName) println(DASHES) println(DASHES) println("6. Add a target to the rule that sends an email to the specified topic.") println("Enter your email to subscribe to the Amazon SNS topic:") val email = sc.nextLine() subEmail(topicArn, email) println("Use the link in the email you received to confirm your subscription. Then press Enter to continue.") sc.nextLine() println(DASHES) println(DASHES) println("7. Create an EventBridge event that sends an email when an Amazon S3 object is created.") addSnsEventRule(eventRuleName, topicArn, topicName, eventRuleName, bucketName) println(DASHES) println(DASHES) println("8. List targets.") listTargets(eventRuleName) println(DASHES) println(DASHES) println(" 9. List the rules for the same target.") listTargetRules(topicArn) println(DASHES) println(DASHES) println("10. Trigger the rule by uploading a file to the S3 bucket.") println("Press Enter to continue.") sc.nextLine() uploadTextFiletoS3(bucketName) println(DASHES) println(DASHES) println("11. Disable a specific rule.") changeRuleState(eventRuleName, false) println(DASHES) println(DASHES) println("12. Check and print the state of the rule.") checkRule(eventRuleName) println(DASHES) println(DASHES) println("13. Add a transform to the rule to change the text of the email.") updateSnsEventRule(topicArn, eventRuleName) println(DASHES) println(DASHES) println("14. Enable a specific rule.") changeRuleState(eventRuleName, true) println(DASHES) println(DASHES) println("15. Trigger the updated rule by uploading a file to the S3 bucket.") println("Press Enter to continue.") sc.nextLine() uploadTextFiletoS3(bucketName) println(DASHES) println(DASHES) println("16. Update the rule to a custom rule pattern.") updateToCustomRule(eventRuleName) println("Updated event rule $eventRuleName to use a custom pattern.") updateCustomRuleTargetWithTransform(topicArn, eventRuleName) println("Updated event target $topicArn.") println(DASHES) println(DASHES) println("17. Send an event to trigger the rule. This will trigger a subscription email.") triggerCustomRule(email) println("Events have been sent. Press Enter to continue.") sc.nextLine() println(DASHES) println(DASHES) println("18. Clean up resources.") println("Do you want to clean up resources (y/n)") val ans = sc.nextLine() if (ans.compareTo("y") == 0) { cleanupResources(topicArn, eventRuleName, bucketName, roleName) } else { println("The resources will not be cleaned up. ") } println(DASHES) println(DASHES) println("The Amazon EventBridge example scenario has successfully completed.") println(DASHES) } suspend fun cleanupResources( topicArn: String?, eventRuleName: String?, bucketName: String?, roleName: String?, ) { println("Removing all targets from the event rule.") deleteTargetsFromRule(eventRuleName) deleteRuleByName(eventRuleName) deleteSNSTopic(topicArn) deleteS3Bucket(bucketName) deleteRole(roleName) } suspend fun deleteRole(roleNameVal: String?) { val policyArnVal = "arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess" val policyRequest = DetachRolePolicyRequest { policyArn = policyArnVal roleName = roleNameVal } IamClient { region = "us-east-1" }.use { iam -> iam.detachRolePolicy(policyRequest) println("Successfully detached policy $policyArnVal from role $roleNameVal") // Delete the role. val roleRequest = DeleteRoleRequest { roleName = roleNameVal } iam.deleteRole(roleRequest) println("*** Successfully deleted $roleNameVal") } } suspend fun deleteS3Bucket(bucketName: String?) { // Remove all the objects from the S3 bucket. val listObjects = ListObjectsRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3Client -> val res = s3Client.listObjects(listObjects) val myObjects = res.contents val toDelete = mutableListOf<ObjectIdentifier>() if (myObjects != null) { for (myValue in myObjects) { toDelete.add( ObjectIdentifier { key = myValue.key }, ) } } val delOb = Delete { objects = toDelete } val dor = DeleteObjectsRequest { bucket = bucketName delete = delOb } s3Client.deleteObjects(dor) // Delete the S3 bucket. val deleteBucketRequest = DeleteBucketRequest { bucket = bucketName } s3Client.deleteBucket(deleteBucketRequest) println("You have deleted the bucket and the objects") } } // Delete the SNS topic. suspend fun deleteSNSTopic(topicArnVal: String?) { val request = DeleteTopicRequest { topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> snsClient.deleteTopic(request) println(" $topicArnVal was deleted.") } } suspend fun deleteRuleByName(ruleName: String?) { val ruleRequest = DeleteRuleRequest { name = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.deleteRule(ruleRequest) println("Successfully deleted the rule") } } 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") } } } } suspend fun triggerCustomRule(email: String) { val json = "{" + "\"UserEmail\": \"" + email + "\"," + "\"Message\": \"This event was generated by example code.\"" + "\"UtcTime\": \"Now.\"" + "}" val entry = PutEventsRequestEntry { source = "ExampleSource" detail = json detailType = "ExampleType" } val eventsRequest = PutEventsRequest { this.entries = listOf(entry) } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putEvents(eventsRequest) } } suspend fun updateCustomRuleTargetWithTransform( topicArn: String?, ruleName: String?, ) { val targetId = UUID.randomUUID().toString() val inputTransformerOb = InputTransformer { inputTemplate = "\"Notification: sample event was received.\"" } val target = Target { id = targetId arn = topicArn inputTransformer = inputTransformerOb } val targetsRequest = PutTargetsRequest { rule = ruleName targets = listOf(target) eventBusName = null } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putTargets(targetsRequest) } } suspend fun updateToCustomRule(ruleName: String?) { val customEventsPattern = "{" + "\"source\": [\"ExampleSource\"]," + "\"detail-type\": [\"ExampleType\"]" + "}" val request = PutRuleRequest { name = ruleName description = "Custom test rule" eventPattern = customEventsPattern } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putRule(request) } } // Update an Amazon S3 object created rule with a transform on the target. suspend fun updateSnsEventRule( topicArn: String?, ruleName: String?, ) { val targetId = UUID.randomUUID().toString() val myMap = mutableMapOf<String, String>() myMap["bucket"] = "$.detail.bucket.name" myMap["time"] = "$.time" val inputTransOb = InputTransformer { inputTemplate = "\"Notification: an object was uploaded to bucket <bucket> at <time>.\"" inputPathsMap = myMap } val targetOb = Target { id = targetId arn = topicArn inputTransformer = inputTransOb } val targetsRequest = PutTargetsRequest { rule = ruleName targets = listOf(targetOb) eventBusName = null } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putTargets(targetsRequest) } } suspend fun checkRule(eventRuleName: String?) { val ruleRequest = DescribeRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.describeRule(ruleRequest) println("The state of the rule is $response") } } suspend fun changeRuleState( eventRuleName: String, isEnabled: Boolean?, ) { if (!isEnabled!!) { println("Disabling the rule: $eventRuleName") val ruleRequest = DisableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.disableRule(ruleRequest) } } else { println("Enabling the rule: $eventRuleName") val ruleRequest = EnableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.enableRule(ruleRequest) } } } // Create and upload a file to an S3 bucket to trigger an event. @Throws(IOException::class) suspend fun uploadTextFiletoS3(bucketName: String?) { val fileSuffix = SimpleDateFormat("yyyyMMddHHmmss").format(Date()) val fileName = "TextFile$fileSuffix.txt" val myFile = File(fileName) val fw = FileWriter(myFile.absoluteFile) val bw = BufferedWriter(fw) bw.write("This is a sample file for testing uploads.") bw.close() val putOb = PutObjectRequest { bucket = bucketName key = fileName body = myFile.asByteStream() } S3Client { region = "us-east-1" }.use { s3Client -> s3Client.putObject(putOb) } } suspend fun listTargetRules(topicArnVal: String?) { val ruleNamesByTargetRequest = ListRuleNamesByTargetRequest { targetArn = topicArnVal } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listRuleNamesByTarget(ruleNamesByTargetRequest) response.ruleNames?.forEach { rule -> println("The rule name is $rule") } } } suspend fun listTargets(ruleName: String?) { val ruleRequest = ListTargetsByRuleRequest { rule = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listTargetsByRule(ruleRequest) response.targets?.forEach { target -> println("Target ARN: ${target.arn}") } } } // Add a rule that triggers an SNS target when a file is uploaded to an S3 bucket. suspend fun addSnsEventRule( ruleName: String?, topicArn: String?, topicName: String, eventRuleName: String, bucketName: String, ) { val targetID = UUID.randomUUID().toString() val myTarget = Target { id = targetID arn = topicArn } val targetsOb = mutableListOf<Target>() targetsOb.add(myTarget) val request = PutTargetsRequest { eventBusName = null targets = targetsOb rule = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putTargets(request) println("Added event rule $eventRuleName with Amazon SNS target $topicName for bucket $bucketName.") } } suspend fun subEmail( topicArnVal: String?, email: String?, ) { val request = SubscribeRequest { protocol = "email" endpoint = email returnSubscriptionArn = true topicArn = topicArnVal } SnsClient { region = "us-east-1" }.use { snsClient -> val result = snsClient.subscribe(request) println(" Subscription ARN: ${result.subscriptionArn}") } } suspend fun createSnsTopic(topicName: String): String? { val topicPolicy = "{" + "\"Version\": \"2012-10-17\"," + "\"Statement\": [{" + "\"Sid\": \"EventBridgePublishTopic\"," + "\"Effect\": \"Allow\"," + "\"Principal\": {" + "\"Service\": \"events.amazonaws.com\"" + "}," + "\"Resource\": \"*\"," + "\"Action\": \"sns:Publish\"" + "}]" + "}" val topicAttributes = mutableMapOf<String, String>() topicAttributes["Policy"] = topicPolicy val topicRequest = CreateTopicRequest { name = topicName attributes = topicAttributes } SnsClient { region = "us-east-1" }.use { snsClient -> val response = snsClient.createTopic(topicRequest) println("Added topic $topicName for email subscriptions.") return response.topicArn } } suspend fun listRules() { val rulesRequest = ListRulesRequest { eventBusName = "default" limit = 10 } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listRules(rulesRequest) response.rules?.forEach { rule -> println("The rule name is ${rule.name}") println("The rule ARN is ${rule.arn}") } } } // Create a new event rule that triggers when an Amazon S3 object is created in a bucket. suspend fun addEventRule( roleArnVal: String?, bucketName: String, eventRuleName: String?, ) { val pattern = """{ "source": ["aws.s3"], "detail-type": ["Object Created"], "detail": { "bucket": { "name": ["$bucketName"] } } }""" val ruleRequest = PutRuleRequest { description = "Created by using the AWS SDK for Kotlin" name = eventRuleName eventPattern = pattern roleArn = roleArnVal } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val ruleResponse = eventBrClient.putRule(ruleRequest) println("The ARN of the new rule is ${ruleResponse.ruleArn}") } } // Set the Amazon S3 bucket notification configuration. suspend fun setBucketNotification(bucketName: String) { val eventBridgeConfig = EventBridgeConfiguration { } val configuration = NotificationConfiguration { eventBridgeConfiguration = eventBridgeConfig } val configurationRequest = PutBucketNotificationConfigurationRequest { bucket = bucketName notificationConfiguration = configuration skipDestinationValidation = true } S3Client { region = "us-east-1" }.use { s3Client -> s3Client.putBucketNotificationConfiguration(configurationRequest) println("Added bucket $bucketName with EventBridge events enabled.") } } // Create an S3 bucket using a waiter. suspend fun createBucket(bucketName: String) { val request = CreateBucketRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3 -> s3.createBucket(request) s3.waitUntilBucketExists { bucket = bucketName } println("$bucketName is ready") } } suspend fun checkBucket(bucketName: String?): Boolean { try { // Determine if the S3 bucket exists. val headBucketRequest = HeadBucketRequest { bucket = bucketName } S3Client { region = "us-east-1" }.use { s3Client -> s3Client.headBucket(headBucketRequest) return true } } catch (e: S3Exception) { System.err.println(e.message) } return false } suspend fun createIAMRole( rolenameVal: String?, polJSON: String?, ): String? { val request = CreateRoleRequest { roleName = rolenameVal assumeRolePolicyDocument = polJSON description = "Created using the AWS SDK for Kotlin" } val rolePolicyRequest = AttachRolePolicyRequest { roleName = rolenameVal policyArn = "arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess" } IamClient { region = "us-east-1" }.use { iam -> val response = iam.createRole(request) iam.attachRolePolicy(rolePolicyRequest) return response.role?.arn } }
-
Per API i dettagli, consulta i seguenti argomenti in riferimento AWS SDKa Kotlin API.
-
Azioni
Il seguente esempio di codice mostra come usare. DeleteRule
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. suspend fun deleteRuleByName(ruleName: String?) { val ruleRequest = DeleteRuleRequest { name = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.deleteRule(ruleRequest) println("Successfully deleted the rule") } }
-
Per API i dettagli, vedi il riferimento DeleteRule AWS
SDKa Kotlin API.
-
Il seguente esempio di codice mostra come usare. DescribeRule
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. suspend fun checkRule(eventRuleName: String?) { val ruleRequest = DescribeRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.describeRule(ruleRequest) println("The state of the rule is $response") } }
-
Per API i dettagli, vedi il riferimento DescribeRule AWS
SDKa Kotlin API.
-
Il seguente esempio di codice mostra come usare. DisableRule
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. suspend fun changeRuleState( eventRuleName: String, isEnabled: Boolean?, ) { if (!isEnabled!!) { println("Disabling the rule: $eventRuleName") val ruleRequest = DisableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.disableRule(ruleRequest) } } else { println("Enabling the rule: $eventRuleName") val ruleRequest = EnableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.enableRule(ruleRequest) } } }
-
Per API i dettagli, vedi il riferimento DisableRule AWS
SDKa Kotlin API.
-
Il seguente esempio di codice mostra come usare. EnableRule
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. suspend fun changeRuleState( eventRuleName: String, isEnabled: Boolean?, ) { if (!isEnabled!!) { println("Disabling the rule: $eventRuleName") val ruleRequest = DisableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.disableRule(ruleRequest) } } else { println("Enabling the rule: $eventRuleName") val ruleRequest = EnableRuleRequest { name = eventRuleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.enableRule(ruleRequest) } } }
-
Per API i dettagli, vedi il riferimento EnableRule AWS
SDKa Kotlin API.
-
Il seguente esempio di codice mostra come usare. ListRuleNamesByTarget
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. suspend fun listTargetRules(topicArnVal: String?) { val ruleNamesByTargetRequest = ListRuleNamesByTargetRequest { targetArn = topicArnVal } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listRuleNamesByTarget(ruleNamesByTargetRequest) response.ruleNames?.forEach { rule -> println("The rule name is $rule") } } }
-
Per API i dettagli, vedi il riferimento ListRuleNamesByTarget AWS
SDKa Kotlin API.
-
Il seguente esempio di codice mostra come usare. ListRules
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. suspend fun listRules() { val rulesRequest = ListRulesRequest { eventBusName = "default" limit = 10 } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listRules(rulesRequest) response.rules?.forEach { rule -> println("The rule name is ${rule.name}") println("The rule ARN is ${rule.arn}") } } }
-
Per API i dettagli, vedi il riferimento ListRules AWS
SDKa Kotlin API.
-
Il seguente esempio di codice mostra come usare. ListTargetsByRule
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. suspend fun listTargets(ruleName: String?) { val ruleRequest = ListTargetsByRuleRequest { rule = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val response = eventBrClient.listTargetsByRule(ruleRequest) response.targets?.forEach { target -> println("Target ARN: ${target.arn}") } } }
-
Per API i dettagli, vedi il riferimento ListTargetsByRule AWS
SDKa Kotlin API.
-
Il seguente esempio di codice mostra come usare. PutEvents
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. suspend fun triggerCustomRule(email: String) { val json = "{" + "\"UserEmail\": \"" + email + "\"," + "\"Message\": \"This event was generated by example code.\"" + "\"UtcTime\": \"Now.\"" + "}" val entry = PutEventsRequestEntry { source = "ExampleSource" detail = json detailType = "ExampleType" } val eventsRequest = PutEventsRequest { this.entries = listOf(entry) } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putEvents(eventsRequest) } }
-
Per API i dettagli, vedi il riferimento PutEvents AWS
SDKa Kotlin API.
-
Il seguente esempio di codice mostra come usare. PutRule
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. Crea una regola pianificata.
suspend fun createScRule( ruleName: String?, cronExpression: String?, ) { val ruleRequest = PutRuleRequest { name = ruleName eventBusName = "default" scheduleExpression = cronExpression state = RuleState.Enabled description = "A test rule that runs on a schedule created by the Kotlin API" } EventBridgeClient { region = "us-west-2" }.use { eventBrClient -> val ruleResponse = eventBrClient.putRule(ruleRequest) println("The ARN of the new rule is ${ruleResponse.ruleArn}") } }
Crea una regola che si attiva quando un oggetto viene aggiunto a un bucket di Amazon Simple Storage Service.
// Create a new event rule that triggers when an Amazon S3 object is created in a bucket. suspend fun addEventRule( roleArnVal: String?, bucketName: String, eventRuleName: String?, ) { val pattern = """{ "source": ["aws.s3"], "detail-type": ["Object Created"], "detail": { "bucket": { "name": ["$bucketName"] } } }""" val ruleRequest = PutRuleRequest { description = "Created by using the AWS SDK for Kotlin" name = eventRuleName eventPattern = pattern roleArn = roleArnVal } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> val ruleResponse = eventBrClient.putRule(ruleRequest) println("The ARN of the new rule is ${ruleResponse.ruleArn}") } }
-
Per API i dettagli, vedi il riferimento PutRule AWS
SDKa Kotlin API.
-
Il seguente esempio di codice mostra come usare. PutTargets
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. // Add a rule that triggers an SNS target when a file is uploaded to an S3 bucket. suspend fun addSnsEventRule( ruleName: String?, topicArn: String?, topicName: String, eventRuleName: String, bucketName: String, ) { val targetID = UUID.randomUUID().toString() val myTarget = Target { id = targetID arn = topicArn } val targetsOb = mutableListOf<Target>() targetsOb.add(myTarget) val request = PutTargetsRequest { eventBusName = null targets = targetsOb rule = ruleName } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putTargets(request) println("Added event rule $eventRuleName with Amazon SNS target $topicName for bucket $bucketName.") } }
Aggiungi un trasformatore di input a una destinazione per una regola.
suspend fun updateCustomRuleTargetWithTransform( topicArn: String?, ruleName: String?, ) { val targetId = UUID.randomUUID().toString() val inputTransformerOb = InputTransformer { inputTemplate = "\"Notification: sample event was received.\"" } val target = Target { id = targetId arn = topicArn inputTransformer = inputTransformerOb } val targetsRequest = PutTargetsRequest { rule = ruleName targets = listOf(target) eventBusName = null } EventBridgeClient { region = "us-east-1" }.use { eventBrClient -> eventBrClient.putTargets(targetsRequest) } }
-
Per API i dettagli, vedi il riferimento PutTargets AWS
SDKa Kotlin API.
-
Il seguente esempio di codice mostra come usare. RemoveTargets
- SDKper Kotlin
-
Nota
c'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice 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") } } } }
-
Per API i dettagli, vedi il riferimento RemoveTargets AWS
SDKa Kotlin API.
-