Skip to content

robertsicoie/gcp-pubsub-samples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Cloud Pub/Sub Sample

Curl

You can use curl to publish to a Pub/Sub topic.

Export the following variables:

export PROJECT=<your project id>
export TOPIC=<your topic> 

First you have to create service account:

gcloud iam service-accounts create curl-publisher

Next set role pubsub.publisher for this service-account:

gcloud projects add-iam-policy-binding $PROJECT --member=serviceAccount:curl-publisher@$PROJECT.iam.gserviceaccount.com --role=roles/pubsub.publisher

Get access token:

export ACCESS_TOKEN=`gcloud auth application-default print-access-token`

Create topic:

curl -H "Authorization: Bearer $ACCESS_TOKEN" -X PUT https://pubsub.googleapis.com/v1/projects/$PROJECT/topics/$TOPIC

Create subscription:

echo {\'topic\':\'projects/$PROJECT/topics/$TOPIC\'} > /tmp/data; curl -H 'content-type: application/json' -H "Authorization: Bearer $ACCESS_TOKEN" -X PUT -d @/tmp/data https://pubsub.googleapis.com/v1/projects/$PROJECT/subscriptions/my-sub

Publish message:

export MESSAGE=`echo This is my message to you-ou-ou | base64`; echo "{'messages': [{'data': '$MESSAGE'}]}" > /tmp/data; curl -H 'content-type: application/json' -H "Authorization: Bearer $ACCESS_TOKEN" -X POST --data @/tmp/data https://pubsub.googleapis.com/v1/projects/$PROJECT/topics/$TOPIC:publish

Pull message:

curl -H 'content-type: application/json' -H "Authorization: Bearer $ACCESS_TOKEN" -X POST -d '{"returnImmediately":"false", "maxMessages":"1"}' https://pubsub.googleapis.com/v1/projects/$PROJECT/subscriptions/my-sub:pull

Acknowledge message:

export ACK_ID=<the message ack_id>
curl -H 'content-type: application/json' -H "Authorization: Bearer $ACCESS_TOKEN" -X POST -d '{"ackIds": ["$ACK_ID"]}' https://pubsub.googleapis.com/v1/projects/$PROJECT/subscriptions/my-sub:acknowledge

Delete topic:

curl -H "Authorization: Bearer $ACCESS_TOKEN" -X DELETE https://pubsub.googleapis.com/v1/projects/$PROJECT/topics/$TOPIC

List topics:

curl -H "Authorization: Bearer $ACCESS_TOKEN" https://pubsub.googleapis.com/v1/projects/$PROJECT/topics

About

Cheat sheet for using curl to work with Cloud Pub/Sub

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published