イベントを直接公開する

Google Cloud CLI を使用するか、Eventarc Publishing REST API にリクエストを送信して、サポートされている形式で CloudEvents イベントを Eventarc Advanced バスに直接パブリッシュできます。Eventarc クライアント ライブラリを使用して、サポートされている言語から Eventarc API にアクセスすることもできます。

始める前に

作業を始める前に、次の手順を完了していることを確認してください。

  1. 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.
  2. Install the Google Cloud CLI.

  3. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

    gcloud init
  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable eventarc.googleapis.com eventarcpublishing.googleapis.com
  8. Install the Google Cloud CLI.

  9. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  10. To initialize the gcloud CLI, run the following command:

    gcloud init
  11. 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with 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_ID with your Google Cloud project name.

  12. Verify that billing is enabled for your Google Cloud project.

  13. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable eventarc.googleapis.com eventarcpublishing.googleapis.com
  14. 直接公開する

    直接公開するイベント メッセージは、CloudEvents 仕様に準拠している必要があります。

    gcloud

    1. ターミナルを開きます。

    2. 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 属性(拡張属性とも呼ばれます)がいくつでも含まれる場合があります。

    例:

    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 メソッドを使用します。

    リクエストのデータを使用する前に、次のように置き換えます。

    • バスの完全なリソース名(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: イベントの識別子。プロデューサーは、sourceid がイベントごとに一意であることを確認する必要があります。
    • CONTENT_TYPE(省略可): data 値のコンテンツ タイプ。JSON 形式のイベントに datacontenttype 属性がない場合、データは application/json メディアタイプに準拠した JSON 値であると見なされます。
    • DATA_PAYLOAD(省略可): datacontenttype で指定されたメディア形式にエンコードされ、dataschema に準拠したイベント ペイロード(これらの属性が存在する場合)。

    リクエストの本文(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
    
    {}
    

    次のステップ