【问题标题】:Write and retrieve files in Heroku and Dockerized fastAPI project在 Heroku 和 Dockerized fastAPI 项目中写入和检索文件
【发布时间】:2022-01-17 19:03:06
【问题描述】:

我正在努力解决这个问题: 我有一个 fastAPI 应用程序,其中包含几个由其他服务调用的路由,我必须为客户建立一个测试环境来试用这些功能。

我已经容器化了整个 api 项目,效果很好。

然后我通过容器堆栈在 Heroku 上发布了整个内容,现在它不会将文件保存在 /response 目录中。

我的 Dockerfile 是:

FROM python:3.9-slim

COPY ./src /app/src

RUN mkdir /app/responses

COPY ./requirements.txt /app
COPY ./start.sh /app
COPY ./templates /app/templates
COPY ./static /app/static


WORKDIR /app

RUN pip install -r requirements.txt

RUN chmod a+x ./start.sh
#EXPOSE 8000

CMD ["./start.sh"]

我的 fastAPI 应用已经挂载了静态文件目录(这也不起作用)和响应目录如下:

from fastapi import FastAPI, File, UploadFile, Request, Form
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates


app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/responses", StaticFiles(directory="responses"), name="responses")

templates = Jinja2Templates(directory="templates")

然后我尝试用这段代码编写文件:

 filename = fiscalCode + "/" + fiscalCode +"_" +timestamp+".html"
    filepath = os.path.join(RESPONSE_FOLDER, filename)

    print(filepath)
    if not os.path.exists(filepath):
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
        with open(filepath, "w+") as f:
            f.write(file.file.read().decode("utf-8"))

我知道 Heroku 文件系统是短暂的,但这不是问题:该文件必须仅可用于测试目的几分钟。

我被困在通过 Heroku 和 Procfile 部署的其他应用程序上(所以没有 Docker 恶作剧)我在编写和检索文件时完全没有问题。

感谢您的任何想法。

【问题讨论】:

  • Heroku 给你什么错误信息?

标签: docker heroku fastapi


【解决方案1】:

我发现,即使在 Heroku 远程 shell 中,我也看不到创建的文件,我认为是因为临时文件系统。

尽管如此,所有功能都运行良好,并且过渡文件存储在超铀的某处。

因此,如果您没有找到实际文件,它们会被写入(我想)缓存内存或类似的东西上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 2012-10-15
    • 2021-09-20
    • 1970-01-01
    相关资源
    最近更新 更多