EventBridge Beispiele mit AWS SDK for .NET - AWS SDK for .NET

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

EventBridge Beispiele mit AWS SDK for .NET

Die folgenden Codebeispiele zeigen Ihnen, wie Sie mithilfe von AWS SDK for .NET with Aktionen ausführen und allgemeine Szenarien implementieren EventBridge.

Basics sind Codebeispiele, die Ihnen zeigen, wie Sie die wichtigsten Operationen innerhalb eines Dienstes ausführen.

Aktionen sind Codeauszüge aus größeren Programmen und müssen im Kontext ausgeführt werden. Aktionen zeigen Ihnen zwar, wie Sie einzelne Servicefunktionen aufrufen, aber Sie können Aktionen im Kontext der zugehörigen Szenarien sehen.

Jedes Beispiel enthält einen Link zum vollständigen Quellcode, in dem Sie Anweisungen zum Einrichten und Ausführen des Codes im Kontext finden.

Erste Schritte

Die folgenden Codebeispiele zeigen, wie Sie mit der Verwendung beginnen EventBridge.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

using Amazon.EventBridge; using Amazon.EventBridge.Model; namespace EventBridgeActions; public static class HelloEventBridge { static async Task Main(string[] args) { var eventBridgeClient = new AmazonEventBridgeClient(); Console.WriteLine($"Hello Amazon EventBridge! Following are some of your EventBuses:"); Console.WriteLine(); // You can use await and any of the async methods to get a response. // Let's get the first five event buses. var response = await eventBridgeClient.ListEventBusesAsync( new ListEventBusesRequest() { Limit = 5 }); foreach (var eventBus in response.EventBuses) { Console.WriteLine($"\tEventBus: {eventBus.Name}"); Console.WriteLine($"\tArn: {eventBus.Arn}"); Console.WriteLine($"\tPolicy: {eventBus.Policy}"); Console.WriteLine(); } } }
  • APIEinzelheiten finden Sie ListEventBusesunter AWS SDK for .NET APIReferenz.

Grundlagen

Wie das aussehen kann, sehen Sie am nachfolgenden Beispielcode:

  • Erstellen Sie eine Regel und fügen Sie ihr ein Ziel hinzu.

  • Aktivieren und deaktivieren Sie Regeln.

  • Listen Sie Regeln und Ziele auf und aktualisieren Sie sie.

  • Senden Sie Ereignisse und bereinigen Sie dann die Ressourcen.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Führen Sie ein interaktives Szenario an einer Eingabeaufforderung aus.

public class EventBridgeScenario { /* Before running this .NET code example, set up your development environment, including your credentials. This .NET example performs the following tasks with Amazon EventBridge: - Create a rule. - Add a target to a rule. - Enable and disable rules. - List rules and targets. - Update rules and targets. - Send events. - Delete the rule. */ private static ILogger logger = null!; private static EventBridgeWrapper _eventBridgeWrapper = null!; private static IConfiguration _configuration = null!; private static IAmazonIdentityManagementService? _iamClient = null!; private static IAmazonSimpleNotificationService? _snsClient = null!; private static IAmazonS3 _s3Client = null!; static async Task Main(string[] args) { // Set up dependency injection for Amazon EventBridge. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonEventBridge>() .AddAWSService<IAmazonIdentityManagementService>() .AddAWSService<IAmazonS3>() .AddAWSService<IAmazonSimpleNotificationService>() .AddTransient<EventBridgeWrapper>() ) .Build(); _configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load settings from .json file. .AddJsonFile("settings.local.json", true) // Optionally, load local settings. .Build(); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<EventBridgeScenario>(); ServicesSetup(host); string topicArn = ""; string roleArn = ""; Console.WriteLine(new string('-', 80)); Console.WriteLine("Welcome to the Amazon EventBridge example scenario."); Console.WriteLine(new string('-', 80)); try { roleArn = await CreateRole(); await CreateBucketWithEventBridgeEvents(); await AddEventRule(roleArn); await ListEventRules(); topicArn = await CreateSnsTopic(); var email = await SubscribeToSnsTopic(topicArn); await AddSnsTarget(topicArn); await ListTargets(); await ListRulesForTarget(topicArn); await UploadS3File(_s3Client); await ChangeRuleState(false); await GetRuleState(); await UpdateSnsEventRule(topicArn); await ChangeRuleState(true); await UploadS3File(_s3Client); await UpdateToCustomRule(topicArn); await TriggerCustomRule(email); await CleanupResources(topicArn); } catch (Exception ex) { logger.LogError(ex, "There was a problem executing the scenario."); await CleanupResources(topicArn); } Console.WriteLine(new string('-', 80)); Console.WriteLine("The Amazon EventBridge example scenario is complete."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Populate the services for use within the console application. /// </summary> /// <param name="host">The services host.</param> private static void ServicesSetup(IHost host) { _eventBridgeWrapper = host.Services.GetRequiredService<EventBridgeWrapper>(); _snsClient = host.Services.GetRequiredService<IAmazonSimpleNotificationService>(); _s3Client = host.Services.GetRequiredService<IAmazonS3>(); _iamClient = host.Services.GetRequiredService<IAmazonIdentityManagementService>(); } /// <summary> /// Create a role to be used by EventBridge. /// </summary> /// <returns>The role Amazon Resource Name (ARN).</returns> public static async Task<string> CreateRole() { Console.WriteLine(new string('-', 80)); Console.WriteLine("Creating a role to use with EventBridge and attaching managed policy AmazonEventBridgeFullAccess."); Console.WriteLine(new string('-', 80)); var roleName = _configuration["roleName"]; var assumeRolePolicy = "{" + "\"Version\": \"2012-10-17\"," + "\"Statement\": [{" + "\"Effect\": \"Allow\"," + "\"Principal\": {" + $"\"Service\": \"events.amazonaws.com\"" + "}," + "\"Action\": \"sts:AssumeRole\"" + "}]" + "}"; var roleResult = await _iamClient!.CreateRoleAsync( new CreateRoleRequest() { AssumeRolePolicyDocument = assumeRolePolicy, Path = "/", RoleName = roleName }); await _iamClient.AttachRolePolicyAsync( new AttachRolePolicyRequest() { PolicyArn = "arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess", RoleName = roleName }); // Allow time for the role to be ready. Thread.Sleep(10000); return roleResult.Role.Arn; } /// <summary> /// Create an Amazon Simple Storage Service (Amazon S3) bucket with EventBridge events enabled. /// </summary> /// <returns>Async task.</returns> private static async Task CreateBucketWithEventBridgeEvents() { Console.WriteLine(new string('-', 80)); Console.WriteLine("Creating an S3 bucket with EventBridge events enabled."); var testBucketName = _configuration["testBucketName"]; var bucketExists = await Amazon.S3.Util.AmazonS3Util.DoesS3BucketExistV2Async(_s3Client, testBucketName); if (!bucketExists) { await _s3Client.PutBucketAsync(new PutBucketRequest() { BucketName = testBucketName, UseClientRegion = true }); } await _s3Client.PutBucketNotificationAsync(new PutBucketNotificationRequest() { BucketName = testBucketName, EventBridgeConfiguration = new EventBridgeConfiguration() }); Console.WriteLine($"\tAdded bucket {testBucketName} with EventBridge events enabled."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Create and upload a file to an S3 bucket to trigger an event. /// </summary> /// <returns>Async task.</returns> private static async Task UploadS3File(IAmazonS3 s3Client) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Uploading a file to the test bucket. This will trigger a subscription email."); var testBucketName = _configuration["testBucketName"]; var fileName = $"example_upload_{DateTime.UtcNow.Ticks}.txt"; // Create the file if it does not already exist. if (!File.Exists(fileName)) { await using StreamWriter sw = File.CreateText(fileName); await sw.WriteLineAsync( "This is a sample file for testing uploads."); } await s3Client.PutObjectAsync(new PutObjectRequest() { FilePath = fileName, BucketName = testBucketName }); Console.WriteLine($"\tPress Enter to continue."); Console.ReadLine(); Console.WriteLine(new string('-', 80)); } /// <summary> /// Create an Amazon Simple Notification Service (Amazon SNS) topic to use as an EventBridge target. /// </summary> /// <returns>Async task.</returns> private static async Task<string> CreateSnsTopic() { Console.WriteLine(new string('-', 80)); Console.WriteLine( "Creating an Amazon Simple Notification Service (Amazon SNS) topic for email subscriptions."); var topicName = _configuration["topicName"]; string topicPolicy = "{" + "\"Version\": \"2012-10-17\"," + "\"Statement\": [{" + "\"Sid\": \"EventBridgePublishTopic\"," + "\"Effect\": \"Allow\"," + "\"Principal\": {" + $"\"Service\": \"events.amazonaws.com\"" + "}," + "\"Resource\": \"*\"," + "\"Action\": \"sns:Publish\"" + "}]" + "}"; var topicAttributes = new Dictionary<string, string>() { { "Policy", topicPolicy } }; var topicResponse = await _snsClient!.CreateTopicAsync(new CreateTopicRequest() { Name = topicName, Attributes = topicAttributes }); Console.WriteLine($"\tAdded topic {topicName} for email subscriptions."); Console.WriteLine(new string('-', 80)); return topicResponse.TopicArn; } /// <summary> /// Subscribe a user email to an SNS topic. /// </summary> /// <param name="topicArn">The ARN of the SNS topic.</param> /// <returns>The user's email.</returns> private static async Task<string> SubscribeToSnsTopic(string topicArn) { Console.WriteLine(new string('-', 80)); string email = ""; while (string.IsNullOrEmpty(email)) { Console.WriteLine("Enter your email to subscribe to the Amazon SNS topic:"); email = Console.ReadLine()!; } var subscriptions = new List<string>(); var paginatedSubscriptions = _snsClient!.Paginators.ListSubscriptionsByTopic( new ListSubscriptionsByTopicRequest() { TopicArn = topicArn }); // Get the entire list using the paginator. await foreach (var subscription in paginatedSubscriptions.Subscriptions) { subscriptions.Add(subscription.Endpoint); } if (subscriptions.Contains(email)) { Console.WriteLine($"\tYour email is already subscribed."); Console.WriteLine(new string('-', 80)); return email; } await _snsClient.SubscribeAsync(new SubscribeRequest() { TopicArn = topicArn, Protocol = "email", Endpoint = email }); Console.WriteLine($"Use the link in the email you received to confirm your subscription, then press Enter to continue."); Console.ReadLine(); Console.WriteLine(new string('-', 80)); return email; } /// <summary> /// Add a rule which triggers when a file is uploaded to an S3 bucket. /// </summary> /// <param name="roleArn">The ARN of the role used by EventBridge.</param> /// <returns>Async task.</returns> private static async Task AddEventRule(string roleArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Creating an EventBridge event that sends an email when an Amazon S3 object is created."); var eventRuleName = _configuration["eventRuleName"]; var testBucketName = _configuration["testBucketName"]; await _eventBridgeWrapper.PutS3UploadRule(roleArn, eventRuleName, testBucketName); Console.WriteLine($"\tAdded event rule {eventRuleName} for bucket {testBucketName}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Add an SNS target to the rule. /// </summary> /// <param name="topicArn">The ARN of the SNS topic.</param> /// <returns>Async task.</returns> private static async Task AddSnsTarget(string topicArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Adding a target to the rule to that sends an email when the rule is triggered."); var eventRuleName = _configuration["eventRuleName"]; var testBucketName = _configuration["testBucketName"]; var topicName = _configuration["topicName"]; await _eventBridgeWrapper.AddSnsTargetToRule(eventRuleName, topicArn); Console.WriteLine($"\tAdded event rule {eventRuleName} with Amazon SNS target {topicName} for bucket {testBucketName}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// List the event rules on the default event bus. /// </summary> /// <returns>Async task.</returns> private static async Task ListEventRules() { Console.WriteLine(new string('-', 80)); Console.WriteLine("Current event rules:"); var rules = await _eventBridgeWrapper.ListAllRulesForEventBus(); rules.ForEach(r => Console.WriteLine($"\tRule: {r.Name} Description: {r.Description} State: {r.State}")); Console.WriteLine(new string('-', 80)); } /// <summary> /// Update the event target to use a transform. /// </summary> /// <param name="topicArn">The SNS topic ARN target to update.</param> /// <returns>Async task.</returns> private static async Task UpdateSnsEventRule(string topicArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Let's update the event target with a transform."); var eventRuleName = _configuration["eventRuleName"]; var testBucketName = _configuration["testBucketName"]; await _eventBridgeWrapper.UpdateS3UploadRuleTargetWithTransform(eventRuleName, topicArn); Console.WriteLine($"\tUpdated event rule {eventRuleName} with Amazon SNS target {topicArn} for bucket {testBucketName}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Update the rule to use a custom event pattern. /// </summary> /// <returns>Async task.</returns> private static async Task UpdateToCustomRule(string topicArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Updating the event pattern to be triggered by a custom event instead."); var eventRuleName = _configuration["eventRuleName"]; await _eventBridgeWrapper.UpdateCustomEventPattern(eventRuleName); Console.WriteLine($"\tUpdated event rule {eventRuleName} to custom pattern."); await _eventBridgeWrapper.UpdateCustomRuleTargetWithTransform(eventRuleName, topicArn); Console.WriteLine($"\tUpdated event target {topicArn}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Send rule events for a custom rule using the user's email address. /// </summary> /// <param name="email">The email address to include.</param> /// <returns>Async task.</returns> private static async Task TriggerCustomRule(string email) { Console.WriteLine(new string('-', 80)); Console.WriteLine("Sending an event to trigger the rule. This will trigger a subscription email."); await _eventBridgeWrapper.PutCustomEmailEvent(email); Console.WriteLine($"\tEvents have been sent. Press Enter to continue."); Console.ReadLine(); Console.WriteLine(new string('-', 80)); } /// <summary> /// List all of the targets for a rule. /// </summary> /// <returns>Async task.</returns> private static async Task ListTargets() { Console.WriteLine(new string('-', 80)); Console.WriteLine("List all of the targets for a particular rule."); var eventRuleName = _configuration["eventRuleName"]; var targets = await _eventBridgeWrapper.ListAllTargetsOnRule(eventRuleName); targets.ForEach(t => Console.WriteLine($"\tTarget: {t.Arn} Id: {t.Id} Input: {t.Input}")); Console.WriteLine(new string('-', 80)); } /// <summary> /// List all of the rules for a particular target. /// </summary> /// <param name="topicArn">The ARN of the SNS topic.</param> /// <returns>Async task.</returns> private static async Task ListRulesForTarget(string topicArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine("List all of the rules for a particular target."); var rules = await _eventBridgeWrapper.ListAllRuleNamesByTarget(topicArn); rules.ForEach(r => Console.WriteLine($"\tRule: {r}")); Console.WriteLine(new string('-', 80)); } /// <summary> /// Enable or disable a particular rule. /// </summary> /// <param name="isEnabled">True to enable the rule, otherwise false.</param> /// <returns>Async task.</returns> private static async Task ChangeRuleState(bool isEnabled) { Console.WriteLine(new string('-', 80)); var eventRuleName = _configuration["eventRuleName"]; if (!isEnabled) { Console.WriteLine($"Disabling the rule: {eventRuleName}"); await _eventBridgeWrapper.DisableRuleByName(eventRuleName); } else { Console.WriteLine($"Enabling the rule: {eventRuleName}"); await _eventBridgeWrapper.EnableRuleByName(eventRuleName); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Get the current state of the rule. /// </summary> /// <returns>Async task.</returns> private static async Task GetRuleState() { Console.WriteLine(new string('-', 80)); var eventRuleName = _configuration["eventRuleName"]; var state = await _eventBridgeWrapper.GetRuleStateByRuleName(eventRuleName); Console.WriteLine($"Rule {eventRuleName} is in current state {state}."); Console.WriteLine(new string('-', 80)); } /// <summary> /// Clean up the resources from the scenario. /// </summary> /// <param name="topicArn">The ARN of the SNS topic to clean up.</param> /// <returns>Async task.</returns> private static async Task CleanupResources(string topicArn) { Console.WriteLine(new string('-', 80)); Console.WriteLine($"Clean up resources."); var eventRuleName = _configuration["eventRuleName"]; if (GetYesNoResponse($"\tDelete all targets and event rule {eventRuleName}? (y/n)")) { Console.WriteLine($"\tRemoving all targets from the event rule."); await _eventBridgeWrapper.RemoveAllTargetsFromRule(eventRuleName); Console.WriteLine($"\tDeleting event rule."); await _eventBridgeWrapper.DeleteRuleByName(eventRuleName); } var topicName = _configuration["topicName"]; if (GetYesNoResponse($"\tDelete Amazon SNS subscription topic {topicName}? (y/n)")) { Console.WriteLine($"\tDeleting topic."); await _snsClient!.DeleteTopicAsync(new DeleteTopicRequest() { TopicArn = topicArn }); } var bucketName = _configuration["testBucketName"]; if (GetYesNoResponse($"\tDelete Amazon S3 bucket {bucketName}? (y/n)")) { Console.WriteLine($"\tDeleting bucket."); // Delete all objects in the bucket. var deleteList = await _s3Client.ListObjectsV2Async(new ListObjectsV2Request() { BucketName = bucketName }); await _s3Client.DeleteObjectsAsync(new DeleteObjectsRequest() { BucketName = bucketName, Objects = deleteList.S3Objects .Select(o => new KeyVersion { Key = o.Key }).ToList() }); // Now delete the bucket. await _s3Client.DeleteBucketAsync(new DeleteBucketRequest() { BucketName = bucketName }); } var roleName = _configuration["roleName"]; if (GetYesNoResponse($"\tDelete role {roleName}? (y/n)")) { Console.WriteLine($"\tDetaching policy and deleting role."); await _iamClient!.DetachRolePolicyAsync(new DetachRolePolicyRequest() { RoleName = roleName, PolicyArn = "arn:aws:iam::aws:policy/AmazonEventBridgeFullAccess", }); await _iamClient!.DeleteRoleAsync(new DeleteRoleRequest() { RoleName = roleName }); } Console.WriteLine(new string('-', 80)); } /// <summary> /// Helper method to get a yes or no response from the user. /// </summary> /// <param name="question">The question string to print on the console.</param> /// <returns>True if the user responds with a yes.</returns> private static bool GetYesNoResponse(string question) { Console.WriteLine(question); var ynResponse = Console.ReadLine(); var response = ynResponse != null && ynResponse.Equals("y", StringComparison.InvariantCultureIgnoreCase); return response; } }

Erstellen Sie eine Klasse, die EventBridge Operationen umschließt.

/// <summary> /// Wrapper for Amazon EventBridge operations. /// </summary> public class EventBridgeWrapper { private readonly IAmazonEventBridge _amazonEventBridge; private readonly ILogger<EventBridgeWrapper> _logger; /// <summary> /// Constructor for the EventBridge wrapper. /// </summary> /// <param name="amazonEventBridge">The injected EventBridge client.</param> /// <param name="logger">The injected logger for the wrapper.</param> public EventBridgeWrapper(IAmazonEventBridge amazonEventBridge, ILogger<EventBridgeWrapper> logger) { _amazonEventBridge = amazonEventBridge; _logger = logger; } /// <summary> /// Get the state for a rule by the rule name. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="eventBusName">The optional name of the event bus. If empty, uses the default event bus.</param> /// <returns>The state of the rule.</returns> public async Task<RuleState> GetRuleStateByRuleName(string ruleName, string? eventBusName = null) { var ruleResponse = await _amazonEventBridge.DescribeRuleAsync( new DescribeRuleRequest() { Name = ruleName, EventBusName = eventBusName }); return ruleResponse.State; } /// <summary> /// Enable a particular rule on an event bus. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <returns>True if successful.</returns> public async Task<bool> EnableRuleByName(string ruleName) { var ruleResponse = await _amazonEventBridge.EnableRuleAsync( new EnableRuleRequest() { Name = ruleName }); return ruleResponse.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Disable a particular rule on an event bus. /// </summary /// <param name="ruleName">The name of the rule.</param> /// <returns>True if successful.</returns> public async Task<bool> DisableRuleByName(string ruleName) { var ruleResponse = await _amazonEventBridge.DisableRuleAsync( new DisableRuleRequest() { Name = ruleName }); return ruleResponse.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// List the rules on an event bus. /// </summary> /// <param name="eventBusArn">The optional ARN of the event bus. If empty, uses the default event bus.</param> /// <returns>The list of rules.</returns> public async Task<List<Rule>> ListAllRulesForEventBus(string? eventBusArn = null) { var results = new List<Rule>(); var request = new ListRulesRequest() { EventBusName = eventBusArn }; // Get all of the pages of rules. ListRulesResponse response; do { response = await _amazonEventBridge.ListRulesAsync(request); results.AddRange(response.Rules); request.NextToken = response.NextToken; } while (response.NextToken is not null); return results; } /// <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; } /// <summary> /// List names of all rules matching a target. /// </summary> /// <param name="targetArn">The ARN of the target.</param> /// <returns>The list of rule names.</returns> public async Task<List<string>> ListAllRuleNamesByTarget(string targetArn) { var results = new List<string>(); var request = new ListRuleNamesByTargetRequest() { TargetArn = targetArn }; ListRuleNamesByTargetResponse response; do { response = await _amazonEventBridge.ListRuleNamesByTargetAsync(request); results.AddRange(response.RuleNames); request.NextToken = response.NextToken; } while (response.NextToken is not null); return results; } /// <summary> /// Create a new event rule that triggers when an Amazon S3 object is created in a bucket. /// </summary> /// <param name="roleArn">The ARN of the role.</param> /// <param name="ruleName">The name to give the rule.</param> /// <param name="bucketName">The name of the bucket to trigger the event.</param> /// <returns>The ARN of the new rule.</returns> public async Task<string> PutS3UploadRule(string roleArn, string ruleName, string bucketName) { string eventPattern = "{" + "\"source\": [\"aws.s3\"]," + "\"detail-type\": [\"Object Created\"]," + "\"detail\": {" + "\"bucket\": {" + "\"name\": [\"" + bucketName + "\"]" + "}" + "}" + "}"; var response = await _amazonEventBridge.PutRuleAsync( new PutRuleRequest() { Name = ruleName, Description = "Example S3 upload rule for EventBridge", RoleArn = roleArn, EventPattern = eventPattern }); return response.RuleArn; } /// <summary> /// Update an Amazon S3 object created rule with a transform on the target. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="targetArn">The ARN of the target.</param> /// <param name="eventBusArn">Optional event bus ARN. If empty, uses the default event bus.</param> /// <returns>The ID of the target.</returns> public async Task<string> UpdateS3UploadRuleTargetWithTransform(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); var targets = new List<Target> { new Target() { Id = targetID, Arn = targetArn, InputTransformer = new InputTransformer() { InputPathsMap = new Dictionary<string, string>() { {"bucket", "$.detail.bucket.name"}, {"time", "$.time"} }, InputTemplate = "\"Notification: an object was uploaded to bucket <bucket> at <time>.\"" } } }; var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; } /// <summary> /// Update a custom rule with a transform on the target. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="targetArn">The ARN of the target.</param> /// <param name="eventBusArn">Optional event bus ARN. If empty, uses the default event bus.</param> /// <returns>The ID of the target.</returns> public async Task<string> UpdateCustomRuleTargetWithTransform(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); var targets = new List<Target> { new Target() { Id = targetID, Arn = targetArn, InputTransformer = new InputTransformer() { InputTemplate = "\"Notification: sample event was received.\"" } } }; var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; } /// <summary> /// Add an event to the event bus that includes an email, message, and time. /// </summary> /// <param name="email">The email to use in the event detail of the custom event.</param> /// <returns>True if successful.</returns> public async Task<bool> PutCustomEmailEvent(string email) { var eventDetail = new { UserEmail = email, Message = "This event was generated by example code.", UtcTime = DateTime.UtcNow.ToString("g") }; var response = await _amazonEventBridge.PutEventsAsync( new PutEventsRequest() { Entries = new List<PutEventsRequestEntry>() { new PutEventsRequestEntry() { Source = "ExampleSource", Detail = JsonSerializer.Serialize(eventDetail), DetailType = "ExampleType" } } }); return response.FailedEntryCount == 0; } /// <summary> /// Update a rule to use a custom defined event pattern. /// </summary> /// <param name="ruleName">The name of the rule to update.</param> /// <returns>The ARN of the updated rule.</returns> public async Task<string> UpdateCustomEventPattern(string ruleName) { string customEventsPattern = "{" + "\"source\": [\"ExampleSource\"]," + "\"detail-type\": [\"ExampleType\"]" + "}"; var response = await _amazonEventBridge.PutRuleAsync( new PutRuleRequest() { Name = ruleName, Description = "Custom test rule", EventPattern = customEventsPattern }); return response.RuleArn; } /// <summary> /// Add an Amazon SNS target topic to a rule. /// </summary> /// <param name="ruleName">The name of the rule to update.</param> /// <param name="targetArn">The ARN of the Amazon SNS target.</param> /// <param name="eventBusArn">The optional event bus name, uses default if empty.</param> /// <returns>The ID of the target.</returns> public async Task<string> AddSnsTargetToRule(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); // Create the list of targets and add a new target. var targets = new List<Target> { new Target() { Arn = targetArn, Id = targetID } }; // Add the targets to the rule. var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; } /// <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; } /// <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> DeleteRuleByName(string ruleName) { var response = await _amazonEventBridge.DeleteRuleAsync( new DeleteRuleRequest() { Name = ruleName }); return response.HttpStatusCode == HttpStatusCode.OK; } }

Aktionen

Das folgende Codebeispiel zeigt die VerwendungDeleteRule.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Löschen Sie eine Regel anhand ihres Namens.

/// <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> DeleteRuleByName(string ruleName) { var response = await _amazonEventBridge.DeleteRuleAsync( new DeleteRuleRequest() { Name = ruleName }); return response.HttpStatusCode == HttpStatusCode.OK; }
  • APIEinzelheiten finden Sie DeleteRuleunter AWS SDK for .NET APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungDescribeRule.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Rufen Sie den Status einer Regel anhand der Regelbeschreibung ab.

/// <summary> /// Get the state for a rule by the rule name. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="eventBusName">The optional name of the event bus. If empty, uses the default event bus.</param> /// <returns>The state of the rule.</returns> public async Task<RuleState> GetRuleStateByRuleName(string ruleName, string? eventBusName = null) { var ruleResponse = await _amazonEventBridge.DescribeRuleAsync( new DescribeRuleRequest() { Name = ruleName, EventBusName = eventBusName }); return ruleResponse.State; }
  • APIEinzelheiten finden Sie DescribeRuleunter AWS SDK for .NET APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungDisableRule.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Deaktivieren Sie eine Regel anhand ihres Regelnamens.

/// <summary> /// Disable a particular rule on an event bus. /// </summary /// <param name="ruleName">The name of the rule.</param> /// <returns>True if successful.</returns> public async Task<bool> DisableRuleByName(string ruleName) { var ruleResponse = await _amazonEventBridge.DisableRuleAsync( new DisableRuleRequest() { Name = ruleName }); return ruleResponse.HttpStatusCode == HttpStatusCode.OK; }
  • APIEinzelheiten finden Sie DisableRuleunter AWS SDK for .NET APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungEnableRule.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Aktivieren Sie eine Regel anhand ihres Regelnamens.

/// <summary> /// Enable a particular rule on an event bus. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <returns>True if successful.</returns> public async Task<bool> EnableRuleByName(string ruleName) { var ruleResponse = await _amazonEventBridge.EnableRuleAsync( new EnableRuleRequest() { Name = ruleName }); return ruleResponse.HttpStatusCode == HttpStatusCode.OK; }
  • APIEinzelheiten finden Sie EnableRuleunter AWS SDK for .NET APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungListRuleNamesByTarget.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Listen Sie alle Regelnamen mithilfe des Ziels auf.

/// <summary> /// List names of all rules matching a target. /// </summary> /// <param name="targetArn">The ARN of the target.</param> /// <returns>The list of rule names.</returns> public async Task<List<string>> ListAllRuleNamesByTarget(string targetArn) { var results = new List<string>(); var request = new ListRuleNamesByTargetRequest() { TargetArn = targetArn }; ListRuleNamesByTargetResponse response; do { response = await _amazonEventBridge.ListRuleNamesByTargetAsync(request); results.AddRange(response.RuleNames); request.NextToken = response.NextToken; } while (response.NextToken is not null); return results; }

Das folgende Codebeispiel zeigt die VerwendungListRules.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Listen Sie alle Regeln für einen Event Bus auf.

/// <summary> /// List the rules on an event bus. /// </summary> /// <param name="eventBusArn">The optional ARN of the event bus. If empty, uses the default event bus.</param> /// <returns>The list of rules.</returns> public async Task<List<Rule>> ListAllRulesForEventBus(string? eventBusArn = null) { var results = new List<Rule>(); var request = new ListRulesRequest() { EventBusName = eventBusArn }; // Get all of the pages of rules. ListRulesResponse response; do { response = await _amazonEventBridge.ListRulesAsync(request); results.AddRange(response.Rules); request.NextToken = response.NextToken; } while (response.NextToken is not null); return results; }
  • APIEinzelheiten finden Sie ListRulesunter AWS SDK for .NET APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungListTargetsByRule.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Listen Sie alle Ziele für eine Regel mithilfe des Regelnamens auf.

/// <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; }

Das folgende Codebeispiel zeigt die VerwendungPutEvents.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Senden Sie ein Ereignis, das einem benutzerdefinierten Muster für eine Regel entspricht.

/// <summary> /// Add an event to the event bus that includes an email, message, and time. /// </summary> /// <param name="email">The email to use in the event detail of the custom event.</param> /// <returns>True if successful.</returns> public async Task<bool> PutCustomEmailEvent(string email) { var eventDetail = new { UserEmail = email, Message = "This event was generated by example code.", UtcTime = DateTime.UtcNow.ToString("g") }; var response = await _amazonEventBridge.PutEventsAsync( new PutEventsRequest() { Entries = new List<PutEventsRequestEntry>() { new PutEventsRequestEntry() { Source = "ExampleSource", Detail = JsonSerializer.Serialize(eventDetail), DetailType = "ExampleType" } } }); return response.FailedEntryCount == 0; }
  • APIEinzelheiten finden Sie PutEventsunter AWS SDK for .NET APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungPutRule.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Erstellen Sie eine Regel, die ausgelöst wird, wenn ein Objekt zu einem Amazon-Simple-Storage-Service-Bucket hinzugefügt wird.

/// <summary> /// Create a new event rule that triggers when an Amazon S3 object is created in a bucket. /// </summary> /// <param name="roleArn">The ARN of the role.</param> /// <param name="ruleName">The name to give the rule.</param> /// <param name="bucketName">The name of the bucket to trigger the event.</param> /// <returns>The ARN of the new rule.</returns> public async Task<string> PutS3UploadRule(string roleArn, string ruleName, string bucketName) { string eventPattern = "{" + "\"source\": [\"aws.s3\"]," + "\"detail-type\": [\"Object Created\"]," + "\"detail\": {" + "\"bucket\": {" + "\"name\": [\"" + bucketName + "\"]" + "}" + "}" + "}"; var response = await _amazonEventBridge.PutRuleAsync( new PutRuleRequest() { Name = ruleName, Description = "Example S3 upload rule for EventBridge", RoleArn = roleArn, EventPattern = eventPattern }); return response.RuleArn; }

Erstellen Sie eine Regel, die ein benutzerdefiniertes Muster verwendet.

/// <summary> /// Update a rule to use a custom defined event pattern. /// </summary> /// <param name="ruleName">The name of the rule to update.</param> /// <returns>The ARN of the updated rule.</returns> public async Task<string> UpdateCustomEventPattern(string ruleName) { string customEventsPattern = "{" + "\"source\": [\"ExampleSource\"]," + "\"detail-type\": [\"ExampleType\"]" + "}"; var response = await _amazonEventBridge.PutRuleAsync( new PutRuleRequest() { Name = ruleName, Description = "Custom test rule", EventPattern = customEventsPattern }); return response.RuleArn; }
  • APIEinzelheiten finden Sie PutRuleunter AWS SDK for .NET APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungPutTargets.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Fügen Sie ein SNS Amazon-Thema als Ziel für eine Regel hinzu.

/// <summary> /// Add an Amazon SNS target topic to a rule. /// </summary> /// <param name="ruleName">The name of the rule to update.</param> /// <param name="targetArn">The ARN of the Amazon SNS target.</param> /// <param name="eventBusArn">The optional event bus name, uses default if empty.</param> /// <returns>The ID of the target.</returns> public async Task<string> AddSnsTargetToRule(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); // Create the list of targets and add a new target. var targets = new List<Target> { new Target() { Arn = targetArn, Id = targetID } }; // Add the targets to the rule. var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; }

Fügen Sie einen Eingabe-Transformator als Ziel für eine Regel hinzu.

/// <summary> /// Update an Amazon S3 object created rule with a transform on the target. /// </summary> /// <param name="ruleName">The name of the rule.</param> /// <param name="targetArn">The ARN of the target.</param> /// <param name="eventBusArn">Optional event bus ARN. If empty, uses the default event bus.</param> /// <returns>The ID of the target.</returns> public async Task<string> UpdateS3UploadRuleTargetWithTransform(string ruleName, string targetArn, string? eventBusArn = null) { var targetID = Guid.NewGuid().ToString(); var targets = new List<Target> { new Target() { Id = targetID, Arn = targetArn, InputTransformer = new InputTransformer() { InputPathsMap = new Dictionary<string, string>() { {"bucket", "$.detail.bucket.name"}, {"time", "$.time"} }, InputTemplate = "\"Notification: an object was uploaded to bucket <bucket> at <time>.\"" } } }; var response = await _amazonEventBridge.PutTargetsAsync( new PutTargetsRequest() { EventBusName = eventBusArn, Rule = ruleName, Targets = targets, }); if (response.FailedEntryCount > 0) { response.FailedEntries.ForEach(e => { _logger.LogError( $"Failed to add target {e.TargetId}: {e.ErrorMessage}, code {e.ErrorCode}"); }); } return targetID; }
  • APIEinzelheiten finden Sie PutTargetsunter AWS SDK for .NET APIReferenz.

Das folgende Codebeispiel zeigt die VerwendungRemoveTargets.

AWS SDK for .NET
Anmerkung

Es gibt noch mehr dazu GitHub. Sie sehen das vollständige Beispiel und erfahren, wie Sie das AWS -Code-Beispiel-Repository einrichten und ausführen.

Entfernen Sie alle Ziele für eine Regel mithilfe des Regelnamens.

/// <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; }
  • APIEinzelheiten finden Sie RemoveTargetsunter AWS SDK for .NET APIReferenz.