FastAPI,return 带有 sql 查询输出的文件响应

FastAPI, return a File response with the output of a sql query

我正在使用 FastAPI,目前我 return 我使用 pandas 从 SQL 服务器读取的 csv。 (pd.read_sql()) 但是 csv 对于浏览器来说相当大,我想 return 它带有文件响应: https://fastapi.tiangolo.com/advanced/custom-response/(页尾)。 如果不先将它写入 csv 文件,我似乎无法做到这一点,该文件看起来很慢,并且会在每次请求时使用 csv 使文件系统混乱。

所以我的问题是,有没有办法从 sql 数据库或 pandas 数据帧 return FileResponse。

如果没有,是否有办法在客户端读取所有生成的 csv 文件后将其删除?

感谢您的帮助!

亲切的问候,

斯蒂芬

很大程度上基于此 https://github.com/tiangolo/fastapi/issues/1277

  1. 将您的数据框变成流
  2. 使用流式响应
  3. 修改headers使其成为下载(可选)
    from fastapi.responses import StreamingResponse
    import io
    
    @app.get("/get_csv")
    async def get_csv():
    
       df = pandas.DataFrame(dict(col1 = 1, col2 = 2))
    
       stream = io.StringIO()
    
       df.to_csv(stream, index = False)
    
       response = StreamingResponse(iter([stream.getvalue()]),
                            media_type="text/csv"
       )
    
       response.headers["Content-Disposition"] = "attachment; filename=export.csv"

       return response

我也是在这个问题上用头撞墙。我的用例略有不同,因为我将图像、pdf 等作为 blob 存储在我的 maria 数据库中。我发现诀窍是将 blob 内容传递给 BytesIO,剩下的很简单。

from fastapi.responses import StreamingResponse
from io import BytesIO

@router.get('/attachment/{id}')
async def get_attachment(id: int):
    mdb = messages(s.MARIADB)

    attachment = mdb.getAttachment(id)
    memfile = BytesIO(attachment['content'])
    response = StreamingResponse(memfile, media_type=attachment['contentType'])
    response.headers["Content-Disposition"] = f"inline; filename={attachment['name']}"

    return response

添加到前面提到的代码中,我发现放置另一个响应 header 很有用,以便客户端能够看到“Content-Disposition”。这是因为客户端默认只能看到 CORS-safelisted 个响应 header。 “Content-Disposition”不是此列表的一部分,因此必须明确添加 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers

我不知道是否有另一种方法可以以更通用的方式为客户端或服务器指定它,以便它适用于所有必要的端点,但这是我应用它的方式。

@router.post("/files", response_class = StreamingResponse)
async def anonymization(file: bytes = File(...), config: str = Form(...)):
    # file as str
    inputFileAsStr = StringIO(str(file,'utf-8'))
    # dataframe
    df = pd.read_csv(inputFileAsStr)
    # send to function to handle anonymization
    results_df = anonymize(df, config)
    # output file
    outFileAsStr = StringIO()
    results_df.to_csv(outFileAsStr, index = False)
    response = StreamingResponse(
        iter([outFileAsStr.getvalue()]),
        media_type='text/csv',
        headers={
            'Content-Disposition': 'attachment;filename=dataset.csv',
            'Access-Control-Expose-Headers': 'Content-Disposition'
        }
    )
    # return
    return response