Skip to content

Instantly share code, notes, and snippets.

@davidwtbuxton
Created December 20, 2018 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwtbuxton/acc676c0136b151c7fb30c8b485e7de6 to your computer and use it in GitHub Desktop.
Save davidwtbuxton/acc676c0136b151c7fb30c8b485e7de6 to your computer and use it in GitHub Desktop.
Sending data to PubSub on App Engine standard 2.7
import base64
import json
# Install google-api-python-client for Py27
import googleapiclient.discovery
def publish(data):
# The PubSub topic is created beforehand.
topic = 'projects/my-app-engine-project/topics/my-topic'
# This will use the service account credentials on App Engine.
service = googleapiclient.discovery.build('pubsub', 'v1')
data_bytes = base64.b64encode(json.dumps(data))
body = {
'messages': [{'data': data_bytes}],
}
request = service.projects().topics().publish(topic=topic, body=body)
response = request.execute()
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment