EventBridge contoh menggunakan AWS SDK for .NET - AWS SDKContoh Kode

Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

EventBridge contoh menggunakan AWS SDK for .NET

Contoh kode berikut menunjukkan cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan AWS SDK for .NET with EventBridge.

Dasar-dasar adalah contoh kode yang menunjukkan kepada Anda bagaimana melakukan operasi penting dalam suatu layanan.

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.

Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.

Memulai

Contoh kode berikut menunjukkan cara untuk mulai menggunakan EventBridge.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

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(); } } }
  • Untuk API detailnya, lihat ListEventBusesdi AWS SDK for .NET APIReferensi.

Hal-hal mendasar

Contoh kode berikut ini menunjukkan cara:

  • Buat aturan dan tambahkan target ke dalamnya.

  • Aktifkan dan nonaktifkan aturan.

  • Daftar dan perbarui aturan dan target.

  • Kirim acara, lalu bersihkan sumber daya.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Jalankan skenario interaktif di penggugah/prompt perintah.

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

Buat kelas yang membungkus EventBridge operasi.

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

Tindakan

Contoh kode berikut menunjukkan cara menggunakanDeleteRule.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Hapus aturan dengan namanya.

/// <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; }
  • Untuk API detailnya, lihat DeleteRuledi AWS SDK for .NET APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanDescribeRule.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Dapatkan status aturan menggunakan deskripsi aturan.

/// <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; }
  • Untuk API detailnya, lihat DescribeRuledi AWS SDK for .NET APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanDisableRule.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Nonaktifkan aturan dengan nama aturannya.

/// <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; }
  • Untuk API detailnya, lihat DisableRuledi AWS SDK for .NET APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanEnableRule.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Aktifkan aturan dengan nama aturannya.

/// <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; }
  • Untuk API detailnya, lihat EnableRuledi AWS SDK for .NET APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanListRuleNamesByTarget.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Daftar semua nama aturan menggunakan target.

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

Contoh kode berikut menunjukkan cara menggunakanListRules.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Daftar semua aturan untuk bus acara.

/// <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; }
  • Untuk API detailnya, lihat ListRulesdi AWS SDK for .NET APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanListTargetsByRule.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Buat daftar semua target untuk aturan menggunakan nama aturan.

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

Contoh kode berikut menunjukkan cara menggunakanPutEvents.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Kirim acara yang cocok dengan pola kustom untuk aturan.

/// <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; }
  • Untuk API detailnya, lihat PutEventsdi AWS SDK for .NET APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanPutRule.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Buat aturan yang dipicu saat objek ditambahkan ke bucket Amazon Simple Storage Service.

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

Buat aturan yang menggunakan pola kustom.

/// <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; }
  • Untuk API detailnya, lihat PutRuledi AWS SDK for .NET APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanPutTargets.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Tambahkan SNS topik Amazon sebagai target aturan.

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

Tambahkan transformator input ke target untuk aturan.

/// <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; }
  • Untuk API detailnya, lihat PutTargetsdi AWS SDK for .NET APIReferensi.

Contoh kode berikut menunjukkan cara menggunakanRemoveTargets.

AWS SDK for .NET
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Hapus semua target untuk aturan menggunakan nama aturan.

/// <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; }
  • Untuk API detailnya, lihat RemoveTargetsdi AWS SDK for .NET APIReferensi.