python - azure 函数连接到 3rd 方 API 在部署时不起作用
问题描述
我有一个简单的 Azure func 定义如下:
import logging
import cdsapi
import azure.functions as func
import sys
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function is about to process request.')
try:
cds = cdsapi.Client()
cds.url="urlgoeshere"
cds.key="keygoeshere"
cds.retrieve("reanalysis-era5-pressure-levels",
{
"variable": "temperature",
"pressure_level": "1000",
"product_type": "reanalysis",
"year": "2008",
"month": "01",
"day": "01",
"time": "12:00",
"format": "grib"
},"download.grib")
return func.HttpResponse(
"This HTTP triggered function executed successfully.",
status_code=200
)
except:
logging.info(" error!", sys.exc_info()[0], "occurred.")
我的目的是在cds.retrieve
命令下检索数据。当我从 Visual Studio 代码调试它时,它工作得很好。但是当我通过 VS 代码将它部署到 Azure 函数时,它根本不起作用。我收到以下错误:
2020-09-02T16:01:45Z [Information] Executing 'Functions.azure-cds' (Reason='This function was programmatically called via the host APIs.', Id=ae0df178-f726-461e-aca7-8ada6995feaa)
2020-09-02T16:01:45Z [Verbose] Sending invocation id:ae0df178-f726-461e-aca7-8ada6995feaa
2020-09-02T16:01:45Z [Verbose] Posting invocation id:ae0df178-f726-461e-aca7-8ada6995feaa on workerId:e365cae7-bb7c-4f14-92b9-82b5cb77334b
2020-09-02T16:01:45Z [Information] Python HTTP trigger function is about to process request.
2020-09-02T16:01:45Z [Error] Executed 'Functions.azure-cds' (Failed, Id=ae0df178-f726-461e-aca7-8ada6995feaa, Duration=9ms)
在requirements.txt
我添加了依赖项:
azure-functions
cdsapi
似乎需要在 azure 门户中以某种方式调整第 3 方 API 解决此问题的任何帮助表示赞赏。
解决方案
为了做到这一点,我必须在cdsapi
包中编辑源代码。/.cdsapirc
这是因为 API 默认设置为在本地计算机中搜索文件。这是存储url
和key
存储的文件。此环境和文件无法在 cloud azure 功能中复制。我所做的只是在类内的方法中设置url
和key
清空字符串,然后将它们设置为适当的值。__init__
Client
推荐阅读
- c# - WebView2 源属性不启动 CoreWebView2
- node.js - Node/Expressjs:如果用户电子邮件已经存在,则停止发布用户图像
- python-3.x - Python Selenium 包,特定语言的选择
- java - 缺少值 Spring-Boot RestController
- r - 错误:无法在 R 中加载外部实体
- javascript - 未捕获的 TypeError:t 未定义,同时尝试通过 ThreeJS ObjectLoader 加载 json
- postgresql - postgresql 存储跨越午夜的开放时间
- javascript - 寻找一种更好的方法来映射对象数组并返回 id 匹配的特定键
- javascript - 阻止键盘从按钮按下弹出
- r - 如何根据三列删除重复项,但我使用 R 保留特定列中编号最高的行?