[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[[["\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)"]]