Vous pouvez publier un événement CloudEvents directement sur un bus Eventarc Advanced dans un format compatible à l'aide de Google Cloud CLI ou en envoyant une requête à l'API REST Eventarc Publishing. Vous pouvez également utiliser les bibliothèques clientes Eventarc pour accéder aux API Eventarc à partir d'un langage compatible.
BUS_NAME : ID ou identifiant complet du bus sur lequel publier l'événement.
Vous ne devez utiliser qu'un seul des éléments suivants :
AVRO_MESSAGE : message d'événement au format Avro, conformément à cette spécification.
JSON_MESSAGE : message d'événement au format JSON, conformément à cette spécification.
DATA_PAYLOAD : données d'un événement publié.
Si vous utilisez l'option --event-data, vous devez également utiliser les options suivantes :
EVENT_ID : identifiant de l'événement. Les producteurs d'événements doivent s'assurer que source + id est unique pour chaque événement distinct.
EVENT_SOURCE : source d'événement d'un événement publié.
EVENT_TYPE : type d'événement lié à l'occurrence d'origine.
Si vous utilisez l'option --event-data, vous pouvez éventuellement utiliser les options suivantes :
EVENT_ATTRIBUTE : attributs d'un événement publié.
Vous pouvez répéter l'option --event-attributes pour ajouter d'autres attributs.
Notez que les événements peuvent inclure un nombre quelconque d'attributs CloudEvents personnalisés supplémentaires avec des noms distincts (également appelés attributs d'extension).
Avant d'utiliser les données de requête, effectuez les remplacements suivants :
Nom complet de la ressource du bus au format
projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAME
Remplacez les éléments suivants :
PROJECT_ID : ID du projet Google Cloud
pour le projet de bus.
LOCATION : région dans laquelle le bus est déployé (par exemple, us-central1).
BUS_NAME : nom du bus sur lequel publier l'événement.
SPEC_VERSION : version de la spécification CloudEvents utilisée par l'événement (par exemple, 1.0).
EVENT_TYPE : type d'événement lié à l'occurrence d'origine.
EVENT_SOURCE : source d'événement d'un événement publié.
EVENT_ID : identifiant de l'événement.
Les producteurs doivent s'assurer que source + id est unique pour chaque événement distinct.
CONTENT_TYPE (facultatif) : type de contenu de la valeur data. Si un événement au format JSON ne comporte pas d'attribut datacontenttype, les données sont considérées comme une valeur JSON conforme au type de support application/json.
DATA_PAYLOAD (facultatif) : charge utile de l'événement encodée au format multimédia spécifié par datacontenttype et respectant dataschema lorsque ces attributs sont présents.
Pour envoyer votre requête, développez l'une des options suivantes :
curl (Linux, macOS ou Cloud Shell)
Enregistrez le corps de la requête dans un fichier nommé request.json.
Exécutez la commande suivante dans le terminal pour créer ou écraser ce fichier dans le répertoire actuel :
Enregistrez le corps de la requête dans un fichier nommé request.json.
Exécutez la commande suivante dans le terminal pour créer ou écraser ce fichier dans le répertoire actuel :
Copiez le corps de la requête et ouvrez la page de référence de la méthode.
Le panneau APIs Explorer s'ouvre dans la partie droite de la page.
Vous pouvez interagir avec cet outil pour envoyer des requêtes.
Collez le corps de la requête dans cet outil, renseignez tous les champs obligatoires, puis cliquez sur Execute (Exécuter).
Si l'opération réussit, le serveur renvoie un code d'état HTTP 200 OK et le corps de réponse vide au format JSON :
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[[["\u003cp\u003eEventarc Advanced allows direct publishing of CloudEvents to an Eventarc bus using the Google Cloud CLI or the Eventarc Publishing REST API.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egcloud beta eventarc message-buses publish\u003c/code\u003e command facilitates event publishing, supporting Avro, JSON, or individual event data with attributes.\u003c/p\u003e\n"],["\u003cp\u003eThe REST API's \u003ccode\u003eprojects.locations.messageBuses.publish\u003c/code\u003e method can be used to publish events, requiring a JSON message body that specifies event attributes like \u003ccode\u003especversion\u003c/code\u003e, \u003ccode\u003etype\u003c/code\u003e, \u003ccode\u003esource\u003c/code\u003e, and \u003ccode\u003eid\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe gcloud CLI can be used through the terminal and the REST API through curl (Linux, macOS, or Cloud Shell), Powershell (Windows), and the API explorer (browser).\u003c/p\u003e\n"],["\u003cp\u003eEventarc Advanced is currently a Pre-GA feature and is subject to limited support.\u003c/p\u003e\n"]]],[],null,["# Publish events directly\n\n[Advanced](/eventarc/advanced/docs/overview)\n\nYou can publish a CloudEvents event directly to an Eventarc Advanced\nbus in a supported format using the Google Cloud CLI or by sending a request to\nthe Eventarc Publishing REST API. You can also use the\n[Eventarc client libraries](/eventarc/docs/reference/libraries)\nto access the Eventarc APIs from a supported language.\n\nBefore you begin\n----------------\n\nIf you haven't already, enable the [Eventarc API](/eventarc/docs/reference/rest)\nand the\n[Eventarc Publishing API](/eventarc/docs/reference/publishing/rest). \n\n```bash\ngcloud services enable eventarc.googleapis.com eventarcpublishing.googleapis.com\n```\n\nPublish directly\n----------------\n\nThe event message you publish directly must conform to the\n[CloudEvents specification](/eventarc/advanced/docs/event-format). \n\n### gcloud\n\n1. Open a terminal.\n\n2. You can publish events to a bus by using the\n [`gcloud eventarc message-buses publish`](/sdk/gcloud/reference/eventarc/message-buses/publish)\n command. For example:\n\n ```bash\n gcloud eventarc message-buses publish BUS_NAME \\\n --avro-message=AVRO_MESSAGE\n ```\n Or: \n\n ```bash\n gcloud eventarc message-buses publish BUS_NAME \\\n --json-message=JSON_MESSAGE\n ```\n Or: \n\n ```bash\n gcloud eventarc message-buses publish BUS_NAME \\\n --event-data=DATA_PAYLOAD \\\n --event-id=EVENT_ID \\\n --event-source=EVENT_SOURCE \\\n --event-type=EVENT_TYPE \\\n --event-attributes=EVENT_ATTRIBUTE\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eBUS_NAME\u003c/var\u003e: the ID or fully qualified identifier of the bus to which to publish the event.\n\n You must use only *one* of the following:\n - \u003cvar translate=\"no\"\u003eAVRO_MESSAGE\u003c/var\u003e: the event message in an Avro format according to this [specification](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/formats/avro-format.md).\n - \u003cvar translate=\"no\"\u003eJSON_MESSAGE\u003c/var\u003e: the event message in a JSON format according to this [specification](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/formats/json-format.md).\n - \u003cvar translate=\"no\"\u003eDATA_PAYLOAD\u003c/var\u003e: the data of a published event.\n\n If using the `--event-data` flag, you must also use the following:\n - \u003cvar translate=\"no\"\u003eEVENT_ID\u003c/var\u003e: the event identifier. Event producers must ensure that `source` + `id` is unique for each distinct event.\n - \u003cvar translate=\"no\"\u003eEVENT_SOURCE\u003c/var\u003e: the event source of a published event.\n - \u003cvar translate=\"no\"\u003eEVENT_TYPE\u003c/var\u003e: the type of event related to the originating occurrence.\n\n If using the `--event-data` flag, you can optionally use the following:\n - \u003cvar translate=\"no\"\u003eEVENT_ATTRIBUTE\u003c/var\u003e: the attributes of a published event.\n You can repeat the `--event-attributes` flag to add more attributes.\n\n Note that events might include any number of additional custom\n CloudEvents attributes with distinct names (also known as\n [extension attributes](https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#extension-context-attributes)).\n\n### Examples:\n\n```bash\ngcloud eventarc message-buses publish my-bus \\\n --event-data='{\"key\": \"hello-world-data\"}' \\\n --event-id=hello-world-id-1234 \\\n --event-source=hello-world-source \\\n --event-type=hello-world-type \\\n --event-attributes=\"datacontenttype=application/json\"\n```\n\n\u003cbr /\u003e\n\n```bash\ngcloud eventarc message-buses publish my-bus --json-message='{\n \"specversion\" : \"1.0\",\n \"type\" :\"com.example.someevent\",\n \"source\" : \"google.cloud.storage.object.v1.finalized\",\n \"id\" : \"A234-1234-1234\",\n \"time\" : \"2024-04-05T17:31:00Z\",\n \"bucket\" : \"bucketName\",\n \"datacontenttype\" : \"application/json\",\n \"data\":{\"key\": \"value\"}\n}'\n```\n\n### REST API\n\nTo publish an event to a bus, use the\n[`projects.locations.messageBuses.publish`](/eventarc/docs/reference/publishing/rest/v1/projects.locations.messageBuses/publish)\nmethod.\n| **Note:** In the `ProtoMessage` of the request body, the `protoData` field only functions as expected when the payload is specifically a `CloudEvent` message type, and can't be used for arbitrary protocol buffer messages. For any other protocol buffer type, you must serialize your proto message into bytes, and use the `binaryData` field instead.\n\n\nBefore using any of the request data,\nmake the following replacements:\n\n- The full resource name of the bus in the format `projects/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`/locations/`\u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e`/messageBuses/`\u003cvar translate=\"no\"\u003eBUS_NAME\u003c/var\u003e\n\n Replace the following:\n - \u003cvar class=\"edit\" scope=\"PROJECT_ID\" translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the Google Cloud project ID for the bus project.\n - \u003cvar class=\"edit\" scope=\"LOCATION\" translate=\"no\"\u003eLOCATION\u003c/var\u003e: the [region](/eventarc/docs/locations) in which the bus is deployed---for example, `us-central1`.\n - \u003cvar class=\"edit\" scope=\"BUS_NAME\" translate=\"no\"\u003eBUS_NAME\u003c/var\u003e: the name of the bus to which to publish the event.\n- \u003cvar class=\"edit\" scope=\"SPEC_VERSION\" translate=\"no\"\u003eSPEC_VERSION\u003c/var\u003e: the version of the CloudEvents specification which the event uses---for example, `1.0`.\n- \u003cvar class=\"edit\" scope=\"EVENT_TYPE\" translate=\"no\"\u003eEVENT_TYPE\u003c/var\u003e: the type of event related to the originating occurrence.\n- \u003cvar class=\"edit\" scope=\"EVENT_SOURCE\" translate=\"no\"\u003eEVENT_SOURCE\u003c/var\u003e: the event source of a published event.\n- \u003cvar class=\"edit\" scope=\"EVENT_ID\" translate=\"no\"\u003eEVENT_ID\u003c/var\u003e: the event identifier. Producers must ensure that `source` + `id` is unique for each distinct event.\n- \u003cvar class=\"edit\" scope=\"CONTENT_TYPE\" translate=\"no\"\u003eCONTENT_TYPE\u003c/var\u003e (optional): the content type of `data` value. If a JSON format event has no `datacontenttype` attribute, then it is assumed that the data is a JSON value conforming to the `application/json` media type.\n- \u003cvar class=\"edit\" scope=\"DATA_PAYLOAD\" translate=\"no\"\u003eDATA_PAYLOAD\u003c/var\u003e (optional): the event payload encoded into the media format specified by `datacontenttype` and adhering to `dataschema` when those attributes are present.\n\n\nRequest JSON body:\n\n```\n{\n\"jsonMessage\":\n \"{\\\"specversion\\\":\\\"SPEC_VERSION\\\",\n \\\"type\\\":\\\"EVENT_TYPE\\\",\n \\\"source\\\":\\\"EVENT_SOURCE\\\",\n \\\"id\\\":\\\"EVENT_ID\\\",\n \\\"datacontenttype\\\":\\\"CONTENT_TYPE\\\",\n \\\"data\\\":\\\"DATA_PAYLOAD\\\"}\"\n}\n```\n\nTo send your request, expand one of these options:\n\n#### curl (Linux, macOS, or Cloud Shell)\n\n| **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](/sdk/gcloud/reference/init) or [`gcloud auth login`](/sdk/gcloud/reference/auth/login) , or by using [Cloud Shell](/shell/docs), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](/sdk/gcloud/reference/auth/list).\n\n\nSave the request body in a file named `request.json`.\nRun the following command in the terminal to create or overwrite\nthis file in the current directory: \n\n```\ncat \u003e request.json \u003c\u003c 'EOF'\n{\n\"jsonMessage\":\n \"{\\\"specversion\\\":\\\"SPEC_VERSION\\\",\n \\\"type\\\":\\\"EVENT_TYPE\\\",\n \\\"source\\\":\\\"EVENT_SOURCE\\\",\n \\\"id\\\":\\\"EVENT_ID\\\",\n \\\"datacontenttype\\\":\\\"CONTENT_TYPE\\\",\n \\\"data\\\":\\\"DATA_PAYLOAD\\\"}\"\n}\nEOF\n```\n\n\nThen execute the following command to send your REST request:\n\n```\ncurl -X POST \\\n -H \"Authorization: Bearer $(gcloud auth print-access-token)\" \\\n -H \"Content-Type: application/json; charset=utf-8\" \\\n -d @request.json \\\n \"https://eventarcpublishing.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAME:publish\"\n```\n\n#### PowerShell (Windows)\n\n| **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](/sdk/gcloud/reference/init) or [`gcloud auth login`](/sdk/gcloud/reference/auth/login) . You can check the currently active account by running [`gcloud auth list`](/sdk/gcloud/reference/auth/list).\n\n\nSave the request body in a file named `request.json`.\nRun the following command in the terminal to create or overwrite\nthis file in the current directory: \n\n```\n@'\n{\n\"jsonMessage\":\n \"{\\\"specversion\\\":\\\"SPEC_VERSION\\\",\n \\\"type\\\":\\\"EVENT_TYPE\\\",\n \\\"source\\\":\\\"EVENT_SOURCE\\\",\n \\\"id\\\":\\\"EVENT_ID\\\",\n \\\"datacontenttype\\\":\\\"CONTENT_TYPE\\\",\n \\\"data\\\":\\\"DATA_PAYLOAD\\\"}\"\n}\n'@ | Out-File -FilePath request.json -Encoding utf8\n```\n\n\nThen execute the following command to send your REST request:\n\n```\n$cred = gcloud auth print-access-token\n$headers = @{ \"Authorization\" = \"Bearer $cred\" }\n\nInvoke-WebRequest `\n -Method POST `\n -Headers $headers `\n -ContentType: \"application/json; charset=utf-8\" `\n -InFile request.json `\n -Uri \"https://eventarcpublishing.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAME:publish\" | Select-Object -Expand Content\n```\n\n#### APIs Explorer (browser)\n\n\nCopy the request body and open the\n\n[method reference page](/eventarc/docs/reference/publishing/rest/v1/projects.locations.messageBuses/publish).\nThe APIs Explorer panel opens on the right side of the page.\nYou can interact with this tool to send requests.\n\nPaste the request body in this tool, complete any other required fields, and click **Execute**.\n\n\nIf successful, the server returns an HTTP `200 OK` status code and the empty response\nbody in JSON format:\n\n```\n200 OK\n\n{}\n```\n\n\u003cbr /\u003e\n\nWhat's next\n-----------\n\n- [Publish events from Google sources](/eventarc/advanced/docs/publish-events/publish-events-google-sources)\n- [Transform received events](/eventarc/advanced/docs/receive-events/transform-events)\n- [Format received events](/eventarc/advanced/docs/receive-events/configure-format-events)"]]