部署 Google 云函数时出现 MissingTargetException
MissingTargetException when deploying Google Cloud Function
我正在尝试在 Python 中部署我的云函数,但我在下面收到此错误。我的函数名称是 function_1
并且在同一基本目录下只有“main.py”和“requirements.txt”。
这是我遇到的错误:
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 829, in __call__
return self.main(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 610, 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 259, in create_app
raise MissingTargetException(
functions_framework.exceptions.MissingTargetException: File /workspace/main.py is expected to contain a function named function_1
编辑:在下面添加 python 代码,如果重要的话,我的入口点名称与函数名称相同。
import requests
import json
from google.cloud import storage
url = "https:/..."
headers = {"Content-Type" : "...",
"Authorization" : "..."}
response = requests.get(url, headers=headers)
json_data = response.json()
pretty_json = json.dumps(json_data, indent=4, sort_keys=True)
storage_client = storage.Client()
bucket = storage_client.bucket("test_bucket")
blob = bucket.blob("test_blob")
blob.upload_from_string(pretty_json)
请指教!
您必须遵守产品代码结构,例如documentation中的示例。
像文件名中那样更新您的代码main.py
import requests
import json
from google.cloud import storage
def function_1(request):
url = "https://api.adalo.com/v0/apps/42a58db3-1093-47bf-a95e-9ceb9405b396/collections/t_8cbeae85d781427988deae1c8bf51d72?offset=0&limit=100"
headers = {"Content-Type" : "application/json",
"Authorization" : "Bearer 7pd6gjjsytfco4yfn3zxc0f1b"}
response = requests.get(url, headers=headers)
json_data = response.json()
pretty_json = json.dumps(json_data, indent=4, sort_keys=True)
storage_client = storage.Client()
bucket = storage_client.bucket("syr_test_bucket1")
blob = bucket.blob("test_blob")
blob.upload_from_string(pretty_json)
部署时,您的函数
gcloud functions deploy function_1 --runtime=python38 --trigger-http <params...>
您的函数(在您的代码中)名称需要与函数(在 gcp 上)名称相同。如果没有,你可以设置一个入口点
gcloud functions deploy function_gcp_name --entry-point=python_function_name --runtime=python38 --trigger-http <params...>
我正在尝试在 Python 中部署我的云函数,但我在下面收到此错误。我的函数名称是 function_1
并且在同一基本目录下只有“main.py”和“requirements.txt”。
这是我遇到的错误:
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 829, in __call__
return self.main(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 610, 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 259, in create_app
raise MissingTargetException(
functions_framework.exceptions.MissingTargetException: File /workspace/main.py is expected to contain a function named function_1
编辑:在下面添加 python 代码,如果重要的话,我的入口点名称与函数名称相同。
import requests
import json
from google.cloud import storage
url = "https:/..."
headers = {"Content-Type" : "...",
"Authorization" : "..."}
response = requests.get(url, headers=headers)
json_data = response.json()
pretty_json = json.dumps(json_data, indent=4, sort_keys=True)
storage_client = storage.Client()
bucket = storage_client.bucket("test_bucket")
blob = bucket.blob("test_blob")
blob.upload_from_string(pretty_json)
请指教!
您必须遵守产品代码结构,例如documentation中的示例。
像文件名中那样更新您的代码main.py
import requests
import json
from google.cloud import storage
def function_1(request):
url = "https://api.adalo.com/v0/apps/42a58db3-1093-47bf-a95e-9ceb9405b396/collections/t_8cbeae85d781427988deae1c8bf51d72?offset=0&limit=100"
headers = {"Content-Type" : "application/json",
"Authorization" : "Bearer 7pd6gjjsytfco4yfn3zxc0f1b"}
response = requests.get(url, headers=headers)
json_data = response.json()
pretty_json = json.dumps(json_data, indent=4, sort_keys=True)
storage_client = storage.Client()
bucket = storage_client.bucket("syr_test_bucket1")
blob = bucket.blob("test_blob")
blob.upload_from_string(pretty_json)
部署时,您的函数
gcloud functions deploy function_1 --runtime=python38 --trigger-http <params...>
您的函数(在您的代码中)名称需要与函数(在 gcp 上)名称相同。如果没有,你可以设置一个入口点
gcloud functions deploy function_gcp_name --entry-point=python_function_name --runtime=python38 --trigger-http <params...>