3
votes

I'm using the python standard environment and would like to publish a message to google pub/sub. However, it appears that the google cloud libraries are not included with the environment, at least without some sort of additional configuration.

from google.cloud import pubsub
ImportError: No module named cloud

This is running on a deployed instance. The example google gives for using pub/sub is in the flexible environment.

1
I considered that approach. My concern was the size (you have to copy all of your libaries into a subdirectory) and the likelyhood something wouldn't work right since everything must be pure-python and some monkey patching is required to get some libraries working correctly in the standard environment.JamesHutchison
They recommend to use an "older" client library if running on standard environment - source. I'm not sure which library they are referring to though. In the worst case, I think you'd have to write a bit of custom code to call their REST API?Kevin Lee

1 Answers

5
votes

App Engine Standard's Python2.7 runtime does not support Pub/Sub Cloud Client Library, only Pub/Sub Service API Client Library. There's some new code samples that shows how to do that.

import googleapiclient.discovery
import base64

service = build('pubsub', 'v1')

topic_path = 'projects/{your_project_id}/topics/{your_topic}'

service.projects().topics().publish(
    topic=topic_path, body={
      "messages": [{
          "data": base64.b64encode(data)
      }]
    }).execute()