首页 > 解决方案 > 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 解决此问题的任何帮助表示赞赏。

标签: pythonazure

解决方案


为了做到这一点,我必须在cdsapi包中编辑源代码。/.cdsapirc这是因为 API 默认设置为在本地计算机中搜索文件。这是存储urlkey存储的文件。此环境和文件无法在 cloud azure 功能中复制。我所做的只是在类内的方法中设置urlkey清空字符串,然后将它们设置为适当的值。__init__Client


推荐阅读