Google Cloud CLI を使用するか、Eventarc Publishing REST API にリクエストを送信して、サポートされている形式で CloudEvents イベントを Eventarc Advanced バスに直接パブリッシュできます。Eventarc クライアント ライブラリを使用して、サポートされている言語から Eventarc API にアクセスすることもできます。
始める前に
作業を始める前に、次の手順を完了していることを確認してください。
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Eventarc and Eventarc Publishing APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable eventarc.googleapis.com
eventarcpublishing.googleapis.com -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Eventarc and Eventarc Publishing APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable eventarc.googleapis.com
eventarcpublishing.googleapis.com ターミナルを開きます。
gcloud eventarc message-buses publishコマンドを使用して、イベントをバスに公開できます。次に例を示します。またはgcloud eventarc message-buses publish BUS_NAME \ --avro-message=AVRO_MESSAGE
またはgcloud eventarc message-buses publish BUS_NAME \ --json-message=JSON_MESSAGE
gcloud eventarc message-buses publish BUS_NAME \ --event-data=DATA_PAYLOAD \ --event-id=EVENT_ID \ --event-source=EVENT_SOURCE \ --event-type=EVENT_TYPE \ --event-attributes=EVENT_ATTRIBUTE
次のように置き換えます。
BUS_NAME: イベントをパブリッシュするバスの ID または完全修飾識別子。
次のいずれか 1 つだけを使用する必要があります。
AVRO_MESSAGE: この仕様に従った Avro 形式のイベント メッセージ。JSON_MESSAGE: この仕様に従った JSON 形式のイベント メッセージ。DATA_PAYLOAD: 公開されたイベントのデータ。
--event-dataフラグを使用する場合は、次のものも使用する必要があります。EVENT_ID: イベント ID。イベント プロデューサーは、source+idがイベントごとに一意であることを確認する必要があります。EVENT_SOURCE: 公開されたイベントのイベントソース。EVENT_TYPE: 元の発生に関連するイベントのタイプ。
--event-dataフラグを使用する場合は、必要に応じて次のものを使用できます。EVENT_ATTRIBUTE: 公開されたイベントの属性。--event-attributesフラグを繰り返して、属性を追加できます。イベントには、異なる名前のカスタム CloudEvents 属性(拡張属性とも呼ばれます)がいくつでも含まれる場合があります。
- バスの完全なリソース名(
projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAME形式)。次のように置き換えます。
PROJECT_ID: バス プロジェクトの Google Cloudプロジェクト ID。LOCATION: バスがデプロイされているリージョン(例:us-central1)。BUS_NAME: イベントを公開するバスの名前。
SPEC_VERSION: イベントで使用される CloudEvents 仕様のバージョン(例:1.0)。EVENT_TYPE: 元の発生に関連するイベントのタイプ。EVENT_SOURCE: 公開されたイベントのイベントソース。EVENT_ID: イベントの識別子。プロデューサーは、sourceとidがイベントごとに一意であることを確認する必要があります。CONTENT_TYPE(省略可):data値のコンテンツ タイプ。JSON 形式のイベントにdatacontenttype属性がない場合、データはapplication/jsonメディアタイプに準拠した JSON 値であると見なされます。DATA_PAYLOAD(省略可):datacontenttypeで指定されたメディア形式にエンコードされ、dataschemaに準拠したイベント ペイロード(これらの属性が存在する場合)。
直接公開する
直接公開するイベント メッセージは、CloudEvents 仕様に準拠している必要があります。
gcloud
例:
gcloud eventarc message-buses publish my-bus \ --event-data='{"key": "hello-world-data"}' \ --event-id=hello-world-id-1234 \ --event-source=hello-world-source \ --event-type=hello-world-type \ --event-attributes="datacontenttype=application/json"
gcloud eventarc message-buses publish my-bus --json-message='{ "specversion" : "1.0", "type" :"com.example.someevent", "source" : "google.cloud.storage.object.v1.finalized", "id" : "A234-1234-1234", "time" : "2024-04-05T17:31:00Z", "bucket" : "bucketName", "datacontenttype" : "application/json", "data":{"key": "value"} }'
REST API
イベントをバスにパブリッシュするには、projects.locations.messageBuses.publish メソッドを使用します。
リクエストのデータを使用する前に、次のように置き換えます。
リクエストの本文(JSON):
{
"jsonMessage":
"{\"specversion\":\"SPEC_VERSION\",
\"type\":\"EVENT_TYPE\",
\"source\":\"EVENT_SOURCE\",
\"id\":\"EVENT_ID\",
\"datacontenttype\":\"CONTENT_TYPE\",
\"data\":\"DATA_PAYLOAD\"}"
}
リクエストを送信するには、次のいずれかのオプションを展開します。
成功した場合、サーバーは HTTP 200 OK ステータス コードと JSON 形式の空のレスポンス本文を返します。
200 OK
{}