PutEvents与 S Amazon DK 或命令行工具配合使用 - Amazon EventBridge
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

PutEvents与 S Amazon DK 或命令行工具配合使用

以下代码示例显示了如何使用PutEvents

操作示例是大型程序的代码摘录,必须在上下文中运行。您可以在以下代码示例中查看此操作的上下文:

.NET
Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

发送与规则的自定义模式相匹配的事件。

/// <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 的详细信息,请参阅 Amazon SDK for .NET API 参考PutEvents中的。

C++
SDK for C++
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

包含所需的文件。

#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 的详细信息,请参阅 Amazon SDK for C++ API 参考PutEvents中的。

CLI
Amazon CLI

向 Events 发送自定义 CloudWatch 事件

此示例向 Events 发送自定义 CloudWatch 事件。该事件包含在 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 的详细信息,请参阅Amazon CLI 命令参考PutEvents中的。

Java
适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

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 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考PutEvents中的。

JavaScript
适用于 JavaScript (v3) 的软件开发工具包
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

导入 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 的详细信息,请参阅 Amazon SDK for JavaScript API 参考PutEvents中的。

适用于 JavaScript (v2) 的软件开发工具包
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

// 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 的详细信息,请参阅 Amazon SDK for JavaScript API 参考PutEvents中的。

Kotlin
适用于 Kotlin 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

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 的详细信息,请参阅适用PutEvents于 K otlin 的Amazon SDK API 参考

有关 S Amazon DK 开发者指南和代码示例的完整列表,请参阅 EventBridge 与 Amazon SDK 一起使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。