You can publish a CloudEvents event directly to an Eventarc Advanced bus in a supported format using the Google Cloud CLI or by sending a request to the Eventarc Publishing REST API. You can also use the Eventarc client libraries to access the Eventarc APIs from a supported language.
The message must conform to the CloudEvents specification.
Open a terminal.
You can publish events to a bus by using the
gcloud beta eventarc message-buses publish
command. For example:gcloud beta eventarc message-buses publish
BUS_NAME \ --avro-message=AVRO_MESSAGE gcloud beta eventarc message-buses publish
BUS_NAME \ --json-message=JSON_MESSAGE gcloud beta 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 Replace the following:
BUS_NAME
: the ID or fully qualified identifier of the bus to which to publish the event.
You must use only one of the following:
AVRO_MESSAGE
: the event message in an Avro format according to this specification.JSON_MESSAGE
: the event message in a JSON format according to this specification.DATA_PAYLOAD
: the data of a published event.
If using the
--event-data
flag, you must also use the following:EVENT_ID
: the event identifier. Event producers must ensure thatsource
+id
is unique for each distinct event.EVENT_SOURCE
: the event source of a published event.EVENT_TYPE
: the type of event related to the originating occurrence.
If using the
--event-data
flag, you can optionally use the following:EVENT_ATTRIBUTE
: the attributes of a published event. You can repeat the--event-attributes
flag to add more attributes.Note that events might include any number of additional custom CloudEvents attributes with distinct names (also known as extension attributes).
Examples:
gcloud beta eventarc message-buses publish my-bus \ --event-id=1234 \ --event-type=event-provider.event.v1.eventType \ --event-source="//event-provider/event/source" \ --event-data='{"key": "value"}' \ --event-attributes=attribute1=value
gcloud beta eventarc message-buses publish my-bus --json-message @- << EOF { "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"} } EOF
To publish an event to a bus, use the
projects.locations.messageBuses.publish
method.
Before using any of the request data, make the following replacements:
- The full resource name of the bus in the format
projects/PROJECT_ID/locations/LOCATION/messageBuses/BUS_NAME
Replace the following:
: the Google Cloud project ID for the bus project.PROJECT_ID
: the region in which the bus is deployed—for example,LOCATION us-central1
.
: the name of the bus to which to publish the event.BUS_NAME
: the version of the CloudEvents specification which the event uses—for example,SPEC_VERSION 1.0
.
: the type of event related to the originating occurrence.EVENT_TYPE
: the event source of a published event.EVENT_SOURCE
: the event identifier. Producers must ensure thatEVENT_ID source
+id
is unique for each distinct event.
(optional): the content type ofCONTENT_TYPE data
value. If a JSON format event has nodatacontenttype
attribute, then it is assumed that the data is a JSON value conforming to theapplication/json
media type.
(optional): the event payload encoded into the media format specified byDATA_PAYLOAD datacontenttype
and adhering todataschema
when those attributes are present.
Request JSON body:
{ "jsonMessage": "{\"specversion\":\"SPEC_VERSION \", \"type\":\"EVENT_TYPE \", \"source\":\"EVENT_SOURCE \", \"id\":\"EVENT_ID \", \"datacontenttype\":\"CONTENT_TYPE \", \"data\":\"DATA_PAYLOAD \"}" }
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell)
Save the request body in a file named request.json
.
Run the following command in the terminal to create or overwrite
this file in the current directory:
cat > request.json << 'EOF' { "jsonMessage": "{\"specversion\":\"SPEC_VERSION \", \"type\":\"EVENT_TYPE \", \"source\":\"EVENT_SOURCE \", \"id\":\"EVENT_ID \", \"datacontenttype\":\"CONTENT_TYPE \", \"data\":\"DATA_PAYLOAD \"}" } EOF
Then execute the following command to send your REST request:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://eventarcpublishing.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /messageBuses/BUS_NAME :publish"
PowerShell (Windows)
Save the request body in a file named request.json
.
Run the following command in the terminal to create or overwrite
this file in the current directory:
@' { "jsonMessage": "{\"specversion\":\"SPEC_VERSION \", \"type\":\"EVENT_TYPE \", \"source\":\"EVENT_SOURCE \", \"id\":\"EVENT_ID \", \"datacontenttype\":\"CONTENT_TYPE \", \"data\":\"DATA_PAYLOAD \"}" } '@ | Out-File -FilePath request.json -Encoding utf8
Then execute the following command to send your REST request:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://eventarcpublishing.googleapis.com/v1/projects/PROJECT_ID /locations/LOCATION /messageBuses/BUS_NAME :publish" | Select-Object -Expand Content
APIs Explorer (browser)
Copy the request body and open the method reference page. The APIs Explorer panel opens on the right side of the page. You can interact with this tool to send requests. Paste the request body in this tool, complete any other required fields, and click Execute.
If successful, the server returns an HTTP 200 OK
status code and the empty response
body in JSON format:
200 OK {}