如何在Google Cloud Functions上部署基本的python函数(带有包)

时间:2019-05-06 06:37:05

标签: python-3.x http google-cloud-platform google-cloud-functions

我正在尝试部署一个基本的python函数,该函数通过Google Functions(浏览器编辑器)使用HTTP触发器调用api。

这是我要部署的功能:

import requests
import json

def call_api():

    API_URL = 'https://some-api.com/v1'
    API_TOKEN = 'some-api-token'

    result = requests.get(API_URL+"/contacts?access_token="+API_TOKEN).json()
    print(result)

call_api()

我的requirements.txt包含:

requests==2.21.0

但是,每次我尝试部署该功能时,都会发生以下错误:

Unknown resource type

我在做什么错?该功能在我的本地计算机上正常工作。

1 个答案:

答案 0 :(得分:1)

有关更多信息,请参阅Writing HTTP Functions。这是我在查看您的代码时想到的:

  • 缺少request参数(def call_api(request):
  • 末尾缺少返回(您无需打印它,只需将其返回给呼叫者)
  • 在文件末尾调用call_api()只会在本地调用该函数,CF不需要此

确保使用gcloud functions deploy call_api --runtime python37 --trigger-http

进行部署