404 API 错误 - 具有托管 Python 函数的 Azure 静态 Web 应用程序

问题描述 投票:0回答:1

我有一个 React Static Web App,在我添加托管 Python Azure Functions 之前它运行良好。在本地运行应用程序时

swa start build --api-location api
,一切都按预期运行,并且可以向 API 发出请求。但是,当使用部署令牌部署到 Azure 时,找不到 API
404

我设法通过从函数代码中删除所有导入来让它“工作”,这让我相信是来自requirements.txt文件的导入没有被安装,导致API不可用。

我通过运行以下命令进行部署:

  1. swa build

  2. swa deploy <config_name> --deployment-token <my_token> --verbose silly

我的swa-cli.config.json文件如下:

{
  "$schema": "https://aka.ms/azure/static-web-apps-cli/schema",
  "configurations": {
    "dml-portal": {
      "appLocation": "./",
      "apiLocation": "api",
      "outputLocation": "build",
      "apiLanguage": "python",
      "apiVersion": "3.10",
      "appBuildCommand": "npm run build",
      "run": "npm start",
      "appDevserverUrl": "http://localhost:3000"
    }
  }
}

我的 React 应用程序构建在

build
文件夹中,我的 API 位于
api
文件夹中。

我的requirements.txt 文件如下所示:

azure-functions
msal
azure-data-tables
azure-core

部署输出中一切看起来都不错:

> Getting config file options from "swa-cli.config.json"...
> Changed directory to C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal
> Found configuration "dml-portal" in "swa-cli.config.json"
> Using configuration "dml-portal" from file:
>   swa-cli.config.json
> Resolving outputLocation=build full path...
> Deploying front-end files from folder:
>   C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\build
> Deploying API from folder:
>   C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\api
> Deployment token provided via flag
>  - --deployment-token: <hidden>
> Deploying to environment: preview
> Trying to read workflow config with values:
>  - appLocation: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal
>  - outputLocation: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\build
>  - apiLocation: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\api
>  - dataApiLocation: <undefined>
> No workflow config folder found at C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\.github\workflows
> Validating user workflow config (BEFORE):
>  - appLocation: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal
>  - outputLocation: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\build
>  - apiLocation: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\api
>  - dataApiLocation: <undefined>
> Validating user workflow config (AFTER):
>  - appLocation: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal
>  - apiLocation: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\api
>  - outputLocation: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\build
>  - dataApiLocation: <undefined>
> No staticwebapp.config.json found in current project
> Deploying project to Azure Static Web Apps...
> Deploying using the following options:
> - env:
>   - SWA_CLI_DEBUG: silly
>   - SWA_RUNTIME_WORKFLOW_LOCATION: <undefined>
>   - SWA_RUNTIME_CONFIG_LOCATION: <undefined>
>   - SWA_RUNTIME_CONFIG: <undefined>
>   - SWA_CLI_VERSION: 2.0.1
>   - SWA_CLI_DEPLOY_DRY_RUN: false
>   - SWA_CLI_DEPLOY_BINARY: C:\Users\Oli\.swa\deploy\19449a00c0269fefc8f29a6d01801c4b19308181\StaticSitesClient.exe@19449a00c0269fefc8f29a6d01801c4b19308181
>   - DEPLOYMENT_ACTION: upload
>   - DEPLOYMENT_PROVIDER: SwaCli
>   - REPOSITORY_BASE: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal
>   - SKIP_APP_BUILD: true
>   - SKIP_API_BUILD: true
>   - DEPLOYMENT_TOKEN: <hidden>
>   - APP_LOCATION: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\build
>   - API_LOCATION: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\api
>   - DATA_API_LOCATION: <undefined>
>   - CONFIG_FILE_LOCATION: <undefined>
>   - VERBOSE: true
>   - FUNCTION_LANGUAGE: python
>   - FUNCTION_LANGUAGE_VERSION: 3.10
>   - DEPLOYMENT_ENVIRONMENT: preview
> ℹ DeploymentId: 852dd5f2-bc2c-4577-ac21-6dasfss2524
> ℹ Verbose logging enabled
> ℹ Try to validate location at: 'C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\build'.
> ℹ Build timeout not specified, defaulting to 15 minutes
> ℹ App Directory Location: 'C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\build' was found.
> ℹ Try to validate location at: 'C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\swa-db-connections'.
> ℹ Looking for event info
> ℹ Event info parsed from action options.
> ℹ Could not get repository branch. Proceeding
> ℹ Could not get repository url. Proceeding
> ℹ Skipping step to build C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\build with Oryx
> ℹ Could not find staticwebapp.config.json file.
> ℹ Didn't find Oryx manifest file under location: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\852dd5f2-bc2c-4577-ac21-6daa399a50b8-swa-oryx\app-manifest\oryx-manifest.toml
> ℹ Determined default file to be: index.html
> ℹ Calculating the size of app artifacts: 2161429 B
> ℹ Try to validate location at: 'C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\C:\Users\Oli\OneDrive - Meganexus\Documents\Code\Projects\dml-portal\api'.
> ℹ Api Directory Location: 'C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\api' was found.
> ℹ Skipping step to build C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\api with Oryx
> ℹ Function Runtime Information. OS: linux, Functions Runtime: ~4, python version: 3.10
> ℹ Zipping Api Artifacts
> ℹ Api Zip will be created from directory: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\api
> ℹ Api Content Hash: a855e46f695619eb1b1fd4c1dd184fe2
> ℹ Done Zipping Api Artifacts
> ℹ Zipping App Artifacts
> ℹ App Zip will be created from directory: C:\Users\Oli\One Drive\Documents\Code\Projects\dml-portal\build
> ℹ Done Zipping App Artifacts
> ℹ Uploading build artifacts.
> ℹ Skipping function upload as functions are identical to last successful deployment.
> ℹ Finished Upload. Polling on deployment.
> ℹ Status: InProgress. Time: 0.0646432(s)
> ℹ Status: Succeeded. Time: 21.2915542(s)
> ℹ Deployment Complete :)
> ℹ Thanks for using Azure Static Web Apps!
> ✔ Project deployed to <my_url> 🚀

我已经被这个问题困扰了很长时间,似乎找不到解决方案。感谢您对此的任何帮助。

编辑1: 我在函数应用程序中添加了一些 Python 代码来准确记录已安装的包。然后,我删除了导入以使 API 正常工作,并部署并检查了 Application Insights 日志。没有安装来自

requirements.txt
的任何软件包,确认问题是requirements.txt 被忽略。

编辑2: 我添加了一个

staticwebapp.config.json
文件:

{
  "routes": [
    {
      "route": "/api/*",
      "allowedRoles": ["anonymous"]
    }
  ]
}

我不确定是否需要向此文件添加任何其他详细信息。

azure azure-functions azure-static-web-app
1个回答
0
投票

我尝试过,在添加托管 Python 函数后,由于 API 文件的问题,可能会在 Azure 静态 Web 应用程序中出现相同的 404 错误。

要解决此问题,请确保创建并激活虚拟环境。


python -m venv venv
venv\Scripts\activate
pip install  -r requirements.txt

我按照本指南使用React应用程序在Azure静态Web应用程序中创建自定义函数。

我参考了此 文档

staticwebapp.config.json
添加到 Azure 静态 Web 应用程序。

staticwebapp.config.json:

{
  "routes": [
    {
      "route": "/api/*",
      "allowedRoles": ["anonymous"]
    }
  ],
  "platform": {
    "apiRuntime": "python"
  }
}

要使用 Azure 静态 Web 应用 CLI 部署 API 服务器并将其添加到静态 Web 应用,我使用了此 资源 和 Azure 静态 Web 应用 CLI 指南

swa-cli.config.json

swa-cli.config.json:

"configurations": {
    "new-folder-(7)": {
      "appLocation": "Hello",
      "apiLocation": "api",
      "outputLocation": ".",
      "appBuildCommand": "npm run build",
      "run": "npm start",
      "appDevserverUrl": "http://localhost:3000"
      "apiLanguage": "python",
      "apiVersion": "3.8",
      "appName": "sampathyth577rat",
      "resourceGroup": "sampathyth577rat-rg"
    }

使用

swa build
构建并使用
swa start
在端口 4000 上本地运行应用程序。

enter image description here

enter image description here

  • 使用
    swa deploy
    部署静态 Web 应用程序。

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.