使用 POST 请求调用外部 HTTP 时 Cloud Functions 超时

Cloud Functions times out when calling an external HTTP with a POST request

我有一个 Google 云函数,在其中我使用 Python requests 调用了两个外部 APIs(或 URLs)图书馆,一个 requests.get 和另一个 requests.post。请注意,这些 API 已使用 Postman 进行测试和工作。

requests.get 正在下载一个 MP3 文件,它正在 Cloud Functions 中运行,因为我能够在 Cloud Storage 中看到下载的文件:

download_url = "https://some.url.com/music.mp3"
resp = requests.get(download_url)
[get the resp.content, put to storage bucket]

除了存储在 Cloud Storage 中,我还将此 mp3 文件发送到 Google Cloud Speech-to-Text API 并获取我想要的转录文本 post 到另一个 URL.

[transcribe the audio using Speech-to-Text API, get the transcibed text]
data = { "download_url": download_url, "transcription": transcribed_text }
upload_api = https://another.url.com/api"
resp = requests.post(upload_api, data = data)

同样,转录工作正常,因为我 print/log 文本显示在 Cloud Functions 的“查看日志”控制台中。然而,在 requests.post 中,我也超时了,也是根据日志。

requests.post(upload_api, data = data, timeout=120)

我什至延长了超时设置,但仍然如此。对此有何解释?是否有 Google 云配置我错过设置可能导致此问题的地方?

这是因为 GCP 云函数有 1 分钟的超时,要解决这个问题,需要在创建函数时声明超时限制

如果您使用 Gcloud CLI 命令部署函数,则需要添加标志 --timeout

例如

gcloud functions deploy FUNCTION_NAME --timeout=TIMEOUT FLAGS

如果您使用的是 GCP 控制台(Web UI),则需要按照此 link

中的步骤操作