Created
December 20, 2018 11:39
-
-
Save davidwtbuxton/acc676c0136b151c7fb30c8b485e7de6 to your computer and use it in GitHub Desktop.
Sending data to PubSub on App Engine standard 2.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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