这是我在部署云功能时得到的错误
File "/layers/google.python.pip/pip/bin/functions-framework", line 8, in <module>
sys.exit(_cli())
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 1130, in __call__
return self.main(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 1055, in main
rv = self.invoke(ctx)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/_cli.py", line 37, in _cli
app = create_app(target, source, signature_type)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 288, in create_app
spec.loader.exec_module(source_module)
File "<frozen importlib._bootstrap_external>", line 843, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/workspace/main.py", line 18, in <module>
bucket = client.get_bucket(const.destination_gcs_bucket)
AttributeError: module 'constants' has no attribute 'destination_gcs_bucket'
我的main.py
的第17和18行是:
client = storage.Client()
bucket = client.get_bucket(const.destination_gcs_bucket)
我的requirements.txt
是:
pandas
constants
google-cloud-storage
google-cloud-bigquery
lxml
flask
datetime
我的constants.py
是:
destination_gcs_bucket = 'nba_teams_data'
我很困惑,因为这个云功能过去部署得很好,我几乎没有改变任何代码,除了一些业务逻辑(regex)。
任何想法?
为什么不使用OS库并将GCS Bucket定义为云函数上的环境变量
代码应该是这样的:
import os
destination_gcs_bucket = os.getenv("DESTINATION_GCS_BUCKET")
其中,云函数上的环境变量定义为:
DESTINATION_GCS_BUCKET = 'nba_teams_data'
这是文档的链接:https://cloud.google.com/functions/docs/configuring/env-var
欢呼,本:)
我没有把常量放在需求中,而只是上传一个文件到Google Cloud Function。然后这样做:
from constants import *