【问题标题】:MissingTargetException when deploying Google Cloud Function部署 Google Cloud Function 时出现 MissingTargetException
【发布时间】:2021-10-17 20:49:06
【问题描述】:

我正在尝试在 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)

请指教!

【问题讨论】:

  • 你能粘贴你的函数python代码吗?
  • 添加了python代码。

标签: python google-cloud-functions google-cloud-storage


【解决方案1】:

您必须遵守产品代码结构,例如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...>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-01
    • 2017-08-04
    • 2020-04-29
    • 2021-05-14
    • 2022-07-15
    • 2021-07-29
    • 2022-11-16
    • 2020-01-22
    相关资源
    最近更新 更多