将 PutEvents
与 AWS SDK 或 CLI 配合使用
以下代码示例演示如何使用 PutEvents
。
操作示例是大型程序的代码摘录,必须在上下文中运行。您可以在以下代码示例中查看此操作的上下文:
- .NET
-
- AWS SDK for .NET
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 发送与规则的自定义模式相匹配的事件。
/// <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; }
-
有关 API 详细信息,请参阅《AWS SDK for .NET API 参考》中的 PutEvents。
-
- C++
-
- SDK for C++
-
注意
查看 GitHub,了解更多信息。查找完整示例,了解如何在 AWS 代码示例存储库
中进行设置和运行。 包含所需的文件。
#include <aws/core/Aws.h> #include <aws/events/EventBridgeClient.h> #include <aws/events/model/PutEventsRequest.h> #include <aws/events/model/PutEventsResult.h> #include <aws/core/utils/Outcome.h> #include <iostream>
发送事件。
Aws::CloudWatchEvents::EventBridgeClient cwe; Aws::CloudWatchEvents::Model::PutEventsRequestEntry event_entry; event_entry.SetDetail(MakeDetails(event_key, event_value)); event_entry.SetDetailType("sampleSubmitted"); event_entry.AddResources(resource_arn); event_entry.SetSource("aws-sdk-cpp-cloudwatch-example"); Aws::CloudWatchEvents::Model::PutEventsRequest request; request.AddEntries(event_entry); auto outcome = cwe.PutEvents(request); if (!outcome.IsSuccess()) { std::cout << "Failed to post CloudWatch event: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully posted CloudWatch event" << std::endl; }
-
有关 API 详细信息,请参阅《AWS SDK for C++ API 参考》中的 PutEvents。
-
- CLI
-
- AWS CLI
-
向 CloudWatch Events 发送自定义事件
以下示例向 CloudWatch Events 发送了自定义事件。该事件包含在 putevents.json 文件中:
aws events put-events --entries
file://putevents.json
以下是 putevents.json 文件的内容:
[ { "Source": "com.mycompany.myapp", "Detail": "{ \"key1\": \"value1\", \"key2\": \"value2\" }", "Resources": [ "resource1", "resource2" ], "DetailType": "myDetailType" }, { "Source": "com.mycompany.myapp", "Detail": "{ \"key1\": \"value3\", \"key2\": \"value4\" }", "Resources": [ "resource1", "resource2" ], "DetailType": "myDetailType" } ]
-
有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 PutEvents
。
-
- Java
-
- SDK for Java 2.x
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 public static void triggerCustomRule(EventBridgeClient eventBrClient, String email) { String json = "{" + "\"UserEmail\": \"" + email + "\"," + "\"Message\": \"This event was generated by example code.\"," + "\"UtcTime\": \"Now.\"" + "}"; PutEventsRequestEntry entry = PutEventsRequestEntry.builder() .source("ExampleSource") .detail(json) .detailType("ExampleType") .build(); PutEventsRequest eventsRequest = PutEventsRequest.builder() .entries(entry) .build(); eventBrClient.putEvents(eventsRequest); }
-
有关 API 详细信息,请参阅《AWS SDK for Java 2.x API 参考》中的 PutEvents。
-
- JavaScript
-
- SDK for JavaScript (v3)
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 导入 SDK 和客户端模块,然后调用 API。
import { EventBridgeClient, PutEventsCommand, } from "@aws-sdk/client-eventbridge"; export const putEvents = async ( source = "eventbridge.integration.test", detailType = "greeting", resources = [], ) => { const client = new EventBridgeClient({}); const response = await client.send( new PutEventsCommand({ Entries: [ { Detail: JSON.stringify({ greeting: "Hello there." }), DetailType: detailType, Resources: resources, Source: source, }, ], }), ); console.log("PutEvents response:"); console.log(response); // PutEvents response: // { // '$metadata': { // httpStatusCode: 200, // requestId: '3d0df73d-dcea-4a23-ae0d-f5556a3ac109', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // Entries: [ { EventId: '51620841-5af4-6402-d9bc-b77734991eb5' } ], // FailedEntryCount: 0 // } return response; };
-
有关 API 详细信息,请参阅《AWS SDK for JavaScript API 参考》中的 PutEvents。
-
- SDK for JavaScript (v2)
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 // Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create CloudWatchEvents service object var ebevents = new AWS.EventBridge({ apiVersion: "2015-10-07" }); var params = { Entries: [ { Detail: '{ "key1": "value1", "key2": "value2" }', DetailType: "appRequestSubmitted", Resources: ["RESOURCE_ARN"], Source: "com.company.app", }, ], }; ebevents.putEvents(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Entries); } });
-
有关 API 详细信息,请参阅《AWS SDK for JavaScript API 参考》中的 PutEvents。
-
- Kotlin
-
- 适用于 Kotlin 的 SDK
-
注意
查看 GitHub,了解更多信息。查找完整示例,学习如何在 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) } }
-
有关 API 详细信息,请参阅《AWS SDK for Kotlin API 参考》中的 PutEvents
。
-
有关 AWS SDK 开发人员指南和代码示例的完整列表,请参阅 将 EventBridge 与 AWS SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。