title: Implementing a cloud function to publish to pubsub triggered by GCS finalize
body: <p>I've been trying to write and deploy a cloud function in Python. (gave up on node.js due to the messy documentation and relatively fast pace of changes)</p>
  
  <p>It is meant to publish a message to a Pub/Sub topic, triggered when a file finishes being uploaded into google cloud bucket ("finalize").</p>
  
  <p>The code I use to deploy the function is</p>
  
  <pre><code>gcloud functions deploy hello_gcs_generic --runtime python37 --trigger-resource bucketcfpubsub
  </code></pre>
  
  <p>I have been trying by using <a href="https://cloud.google.com/pubsub/docs/publisher#pubsub-publish-message-python" rel="noreferrer">this script provided by Google</a></p>
  
  <pre><code>import time
  
  from google.cloud import pubsub_v1
  
  project_id = "bucketcfpubsub"
  topic_name = "projects/bucketcfpubsub/topics/pubsub"
  
  publisher = pubsub_v1.PublisherClient()
  topic_path = publisher.topic_path(project_id, topic_name)
  
  def callback(message_future):
  # When timeout is unspecified, the exception method waits indefinitely.
  if message_future.exception(timeout=30):
      print('Publishing message on {} threw an Exception {}.'.format(
          topic_name, message_future.exception()))
  else:
      print(message_future.result())
  
  for n in range(1, 10):
      data = u'Message number {}'.format(n)
  # Data must be a bytestring
      data = data.encode('utf-8')
  # When you publish a message, the client returns a Future.
      message_future = publisher.publish(topic_path, data=data)
      message_future.add_done_callback(callback)
  
  print('Published message IDs:')
  
  # We must keep the main thread from exiting to allow it to process
  # messages in the background.
  while True:
      time.sleep(60)
  </code></pre>
  
  <p>To which I receive these errors in Google Cloud Console</p>
  
  <pre><code>ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function load error: Code in file main.py can't be loaded.
  Detailed stack trace: Traceback (most recent call last):
    File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 256, in check_or_load_user_function
      _function_handler.load_user_function()
    File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 166, in load_user_function
      spec.loader.exec_module(main)
    File "&lt;frozen importlib._bootstrap_external&gt;", line 728, in exec_module
    File "&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
    File "&lt;frozen importlib._bootstrap_external&gt;", line 728, in exec_module
    File "&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
    File "/user_code/main.py", line 3, in &lt;module&gt;
      from google.cloud import pubsub_v1
  ImportError: cannot import name 'pubsub_v1' from 'google.cloud' (unknown location)
  </code></pre>
  
  <p>Following instruction from <a href="https://stackoverflow.com/questions/52452269/google-cloud-function-importerror-cannot-import-name-pubsub-from-google-cl/">these</a> <a href="https://stackoverflow.com/questions/47011713/importerror-cannot-import-name-pubsub-v1">two</a> posts, I've copied the requirements.txt from the helloworld code sample, containing just </p>
  
  <pre><code>google-cloud-error-reporting==0.30.0
  </code></pre>
  
  <p>and updated other cloud functions like bigquery, storage, and logging. I then got these errors:</p>
  
  <pre><code>ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function load error: Code in file main.py can't be loaded.
  Detailed stack trace: Traceback (most recent call last):
    File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 256, in check_or_load_user_function
      _function_handler.load_user_function()
    File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 166, in load_user_function
      spec.loader.exec_module(main)
    File "&lt;frozen importlib._bootstrap_external&gt;", line 728, in exec_module
    File "&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
    File "/user_code/main.py", line 3, in &lt;module&gt;
  from google.cloud import pubsub_v1`
  </code></pre>
  
  <p>and I also found [this thread](<a href="https://stackoverflow.com/questions/52636483/importerror-cannot-import-name-pubsub-v1-from-google-cloud-unknown-">ImportError: cannot import name &#39;pubsub_v1&#39; from &#39;google.cloud&#39; (unknown location)</a> but I don't really understand what's the solution, I've tried replacing pubsub_v1 with google-cloud-pubsub==0.38.0 which did not help. I get this error instead:</p>
  
  <pre><code>Deploying function (may take a while - up to 2 minutes)...failed.
  ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function load error: Code in file main.py can't be loaded.
  Detailed stack trace: Traceback (most recent call last):
    File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 256, in check_or_load_user_function
      _function_handler.load_user_function()
    File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/worker.py", line 166, in load_user_function
      spec.loader.exec_module(main)
    File "&lt;frozen importlib._bootstrap_external&gt;", line 724, in exec_module
    File "&lt;frozen importlib._bootstrap_external&gt;", line 860, in get_code
    File "&lt;frozen importlib._bootstrap_external&gt;", line 791, in source_to_code
    File "&lt;frozen importlib._bootstrap&gt;", line 219, in _call_with_frames_removed
    File "/user_code/main.py", line 3
  </code></pre>
  
  <p>Also, this does not seem to be a sustainable fix if the code will break once Google updates pubsub to a new version?</p>
  
  <p>So I'm very much a beginner and quite lost, but I hope this documentation could help you guys help me. </p>
  
  <p>UPDATE:</p>
  
  <p>It seems pubsub and pubsub_v1 are both valid to be used, not sure what's the difference,  though. </p>
  
  <p>@dustin I did a pip install -r requirements.txt which ended up matchiing what you provided. 
  I've also noticed an error in deploying the function as 'hello-gcs-generic', which should be changed to 'callback'.</p>
  
  <p>The python code now runs well locally, but deploying it to the cloud using the above code (the first code line in the OP) consistently returns this error</p>
  
  <pre><code>ERROR: (gcloud.functions.deploy) OperationError: code=3, messa
  ge=Function load error: Error: function load attempt timed out
  .
  </code></pre>