PutEvents 搭配 AWS SDK或 使用 CLI - Amazon EventBridge

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

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詳細資訊,請參閱 參考 PutEvents中的 。 AWS SDK for .NET API

C++
SDK 適用於 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詳細資訊,請參閱 參考 PutEvents中的 。 AWS SDK for C++ API

CLI
AWS CLI

將自訂事件傳送至 CloudWatch 事件

此範例會將自訂事件傳送至 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詳細資訊,請參閱 命令參考 PutEvents中的 。 AWS CLI

Java
SDK 適用於 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詳細資訊,請參閱參考 PutEvents中的 AWS SDK for Java 2.x API

JavaScript
SDK 適用於 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詳細資訊,請參閱參考 PutEvents中的 AWS SDK for JavaScript API

SDK 適用於 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詳細資訊,請參閱 參考 PutEvents中的 。 AWS SDK for JavaScript API

Kotlin
SDK 適用於 Kotlin
注意

還有更多 。 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詳細資訊,請參閱PutEvents中的 AWS SDK for Kotlin API參考

如需開發人員指南和程式碼範例的完整清單 AWS SDK,請參閱 EventBridge 搭配 使用 AWS SDK。本主題也包含有關入門的資訊,以及先前SDK版本的詳細資訊。