我有下面的代码,它在本地启动时可以无故障地运行。我输入URL,该函数通过从API检索TFL有效负载并将数据存储在blob存储中来执行其任务。此过程通过访问URL http://localhost:7071/api/tfl_blob触发。
尽管如此,在将此代码部署到Azure平台时,部署过程不会出现任何错误消息,但函数不会出现在函数应用程序中。虽然我可以访问应用程序文件,但我在触发它时遇到了困难,当我导航到功能概述页面时,该功能明显缺失。
问题可能是什么以及如何解决?
功能代码:
import datetime
import json
import requests
import azure.functions as func
import logging
import uuid
app = func.FunctionApp()
def generate_blob_path():
current_datetime = datetime.datetime.now()
date_str = current_datetime.strftime('%Y-%m-%d')
unique_id = str(uuid.uuid4())
blob_path = f"bloboutput/{date_str}/{unique_id}.json"
return blob_path
_blob_path = generate_blob_path()
@app.function_name(name="HttpTflBlobOut")
@app.route(route="tfl_blob")
@app.blob_output(arg_name="blob",
path=_blob_path,
connection="AzureWebJobsStorage")
def main(req: func.HttpRequest, blob: func.Out[str]) -> func.HttpResponse:
try:
result = requests.get('https://api.tfl.gov.uk/line/mode/tube/status',
headers={"content-type": "application/json", "charset": "utf-8"})
logging.info('Python HTTP trigger function processed the API request to TFL')
since_id = None
timeline = json.loads(result.text)
tflLineStatus = []
for t in timeline:
if since_id is None:
since_id = t["id"]
tflLineStatus.append({
"linename": t["id"],
"linestatus": t["lineStatuses"][0]["statusSeverityDescription"],
"timestamp": t["created"]
})
if since_id is None:
since_id = req.state.since_id
ans = {
"state": {
since_id: since_id
},
"schema": {
"tflLineStatus": {
"primary_key": ["linename"]
}
},
"insert": {
"tflLineStatus": tflLineStatus
},
"hasMore": False
}
logging.info('Payload is constructed')
payload = json.dumps(ans, indent=4)
logging.info('Payload is converted to a string')
blob.set(payload)
return func.HttpResponse(f"Function executed successfully", status_code=200)
except Exception as e:
logging.error(f"Error: {str(e)}")
return func.HttpResponse("Internal Server Error", status_code=500)
host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
"AzureWebJobsStorage": <match with storage account connection string and also set on the az function>,
"FUNCTIONS_EXTENSION_VERSION": "~4",
"APPLICATIONINSIGHTS_CONNECTION_STRING": "<match with the function config on az>"
},
"ConnectionStrings": {}
}
我的期望是,我应该能够通过HTTP请求调用函数,使用以下URL格式:https://<function app name>.azurewebsites.net/api/<function name>?code=<function key>
。它的表现应该与它在本地的表现相似。但是,我遇到了一个HTTP错误404,表明无法找到该页面。
1条答案
按热度按时间fykwrbwg1#
*我在我的环境中尝试了你的代码。我能够运行函数代码并部署它。
编码:
local.settings.json:
requirements.txt:
输出:
上面的代码成功运行如下:
浏览器输出:
Azure Portal:
已在存储帐户容器中成功创建Blob,如下所示:
按照以下步骤将上述代码部署到函数应用程序中。
从列表中选择要部署的函数应用。
成功部署如下:
我可以在Azure Portal的函数应用程序中看到如下函数:
我运行了函数代码,它成功执行如下:
然后复制函数输出URL如下:
我还可以看到函数输出带有URL。