【问题标题】:heroku shows file not found / 500 for files that exist in file systemheroku 显示文件未找到 / 500 用于文件系统中存在的文件
【发布时间】:2021-07-15 17:19:42
【问题描述】:

我有一个可以在本地正常工作的烧瓶 + react 应用程序。当我部署到 heroku 时,尝试从 Flask 加载 metrics.yaml 文件时出现 500 错误:

app = Flask(__name__, static_folder='../build', static_url_path='/')

@app.route('/')
def index():
  metrics = load_metrics(url_for('static', filename='metrics.yaml'))

这会导致 500 服务器错误并且整个站点无法加载。 heroku logs --tail 透露:

No such file or directory: '/metrics.yaml'

2021-07-15T16:44:56.490218+00:00 heroku[router]: at=info method=GET path="/metrics.yaml" host=react-flask-heroku-bp.herokuapp.com request_id=6c5561ad-e8b4-49da-bba0-5342052a19a0 fwd="73.25.235.134" dyno=web.1 connect=1ms service=45ms status=200 bytes=1320 protocol=https
2021-07-15T16:49:45.626217+00:00 heroku[router]: at=info method=GET path="/" host=react-flask-heroku-bp.herokuapp.com request_id=48f85a02-744c-4c1e-a52f-f063ee1213e8 fwd="73.25.235.134" dyno=web.1 connect=1ms service=17ms status=500 bytes=463 protocol=https

但如果我将浏览器指向https://[site].herokuapp.com/metrics.yaml 文件,我可以正常查看。

让我感到困惑的是method=GET path="/metrics.yaml" 似乎显示200 状态,然后method=GET path="/" 显示500 错误。这是为什么呢?

如何解决此问题,以便从 Web 根目录加载 /metrics.yaml 文件而不会出现 500 错误?

---更新---

我注意到无论我在这里输入什么地址,即使是绝对路径:https:/mhtransfers.s3.us-west-2.amazonaws.com/metrics.yaml

结果

FileNotFoundError: [Errno 2] No such file or directory: 'https:/mhtransfers.s3.us-west-2.amazonaws.com/metrics.yaml'

这是构建文件夹结构:

【问题讨论】:

  • 你能分享一个存储库的链接吗?没有设置很难回答。
  • 也可以分享一下文件夹结构
  • @MoritzMakowski - 仓库在这里:github.com/heaversm/react-flask-heroku-bp
  • @mheavers 也许您可以尝试打印出url_for('static', filename='.') 以查看它指向的位置。我曾经部署到heroku并像那样组织flask+react。然而,现在我已经切换到使用两个独立的代码库和两个不同的服务器。在 heroku 上,这可能会更贵,因为你只能购买“dynos”。但现在我将我所有的东西都托管在 Google Cloud Run、AWS Fargate(两个完全托管的容器)或 Netlify(用于 Gatsby 站点)上。我强烈建议您研究这些选项,因为我认为这种设置更清洁 - 也更便宜。
  • 还有一件事:使用heroku local,您可以像在 Procfile 中描述的那样在本地运行应用程序。 devcenter.heroku.com/articles/heroku-local 也许这有帮助。

标签: python flask heroku


【解决方案1】:

我设法通过以下方式解决了这个问题:

from pathlib import Path
metrics_file = Path(__file__).parent / "metrics.yaml"
metrics = load_metrics(metrics_file)

这让我可以在没有环境特定变量的情况下定位正确的路径,并在服务器和本地使用相对路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-11
    • 2012-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    相关资源
    最近更新 更多