python - 实现由 GCS 触发的云功能以发布到 pubsub

标签 python google-cloud-platform google-cloud-storage google-cloud-functions google-cloud-pubsub

我一直在尝试用 Python 编写和部署云函数。 (由于文档杂乱且更改速度相对较快,因此放弃了 node.js)

它旨在将消息发布到 Pub/Sub 主题,当文件完成上传到谷歌云存储桶(“完成”)时触发。

我用来部署函数的代码是

gcloud functions deploy hello_gcs_generic --runtime python37 --trigger-resource bucketcfpubsub

我一直在尝试使用 this script provided by Google
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)

我在 Google Cloud Console 中收到这些错误
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 "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/user_code/main.py", line 3, in <module>
    from google.cloud import pubsub_v1
ImportError: cannot import name 'pubsub_v1' from 'google.cloud' (unknown location)

遵循 these 的说明two帖子,我从 helloworld 代码示例中复制了 requirements.txt,仅包含
google-cloud-error-reporting==0.30.0

并更新了其他云功能,如 bigquery、存储和日志记录。然后我得到了这些错误:
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 "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/user_code/main.py", line 3, in <module>
from google.cloud import pubsub_v1`

我还发现了 [this thread]( ImportError: cannot import name 'pubsub_v1' from 'google.cloud' (unknown location) 但我真的不明白解决方案是什么,我试过用 google-cloud-pubsub==0.38.0 替换 pubsub_v1 没有帮助。我得到了这个错误:
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 "<frozen importlib._bootstrap_external>", line 724, in exec_module
  File "<frozen importlib._bootstrap_external>", line 860, in get_code
  File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/user_code/main.py", line 3

此外,如果一旦 Google 将 pubsub 更新到新版本,代码就会中断,这似乎不是一个可持续的解决方案?

所以我是一个初学者并且很迷茫,但我希望这个文档可以帮助你们帮助我。

更新:

似乎 pubsub 和 pubsub_v1 都可以使用,但不确定有什么区别。

@dustin我做了一个 pip install -r requirements.txt 最终匹配你提供的内容。
我还注意到将函数部署为“hello-gcs-generic”时出错,应将其更改为“callback”。

python 代码现在在本地运行良好,但使用上面的代码(OP 中的第一行代码)将其部署到云始终会返回此错误
ERROR: (gcloud.functions.deploy) OperationError: code=3, messa
ge=Function load error: Error: function load attempt timed out
.

最佳答案

您需要添加 google-cloud-pubsub给您的 requirements.txt文件,不在您的 main.py 中文件。它应该是这样的:

google-cloud-error-reporting==0.30.0
google-cloud-pubsub==0.38.0

关于python - 实现由 GCS 触发的云功能以发布到 pubsub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53645432/

相关文章:

google-cloud-storage - Firebase 存储 - 通过 Python google-cloud-storage lib 上传 png 图像时出错

php - 从 Python 发送 HTTP POST 请求(尝试从 PHP 转换)

python - 子目录中的 Django - 管理站点不工作

google-app-engine - console.developers.google.com 与 appengine.google.com 有何不同?

google-maps - Ionic3 ionic原生谷歌地图不显示 map ,只显示谷歌 Logo (显示灰色空白 map )

node.js - Firebase 存储 : Image file types not showing after upload. 如何使用该镜像?

google-cloud-storage - 与 CDN 的 Google CDN 连接不会产生类似的关键错误

python - 当折线图不从第一个 x 轴刻度开始时,如何使 x 轴刻度之间的间距保持一致

python - HTTP 代理服务器 python [客户端帮助]

ruby-on-rails - App Engine Standard Ruby 2.5 上 Rails 6 应用程序的安全配置