尝试通过Azure Functions Post-Deployment访问FastAPI端点时出现404错误

svmlkihl  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(80)

我正在尝试在Azure函数应用上托管FastAPi。我能够在没有错误的情况下部署,但是当我尝试测试端点访问时,我得到了404错误。
我的根文件夹架构看起来像这样:

“function_app.py”中的Azure函数代码如下所示:

import azure.functions as func

from WrapperFunction import app as fastapi_app

app = func.AsgiFunctionApp(app=fastapi_app, http_auth_level=func.AuthLevel.ANONYMOUS)

Azure函数代码从“Wrapper Function”文件夹导入FastAPI应用。FastAPI应用程序代码写在“Wrapper Function”文件夹中的“init.py”文件中,看起来像这样:

from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
origins = ["*"]

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/sample")
def read_sample():
    return{'Hello':'Sample'}

“host.json”文件看起来像这样:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 4.0.0]"
  },
  "extensions": 
  {
    "http": 
    {
        "routePrefix": ""
    }
  }
}

我基本上试图遵循页面上给出的确切程序:https://github.com/Azure-Samples/fastapi-on-azure-functions/.当尝试通过http://. azurewebsites.net/访问部署的azure函数时,它可以正常工作,并向我显示函数应用程序已经启动并运行。
但是,在尝试通过“/sample”访问FastAPI端点时(例如http://. azurewebsites.net/sample),它会抛出404错误。请帮帮我

bcs8qyzn

bcs8qyzn1#

对于任何遇到类似情况的人,我必须在Azure门户中的函数应用程序中的“配置”下添加一个设置,如下所示:
https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-python?pivots=python-mode-decorators#update-app-settings

相关问题