Skip to content
Advertisement

FastAPI serving static files through symlinks

I have mounted the static directory in my FastAPI app using the following code:

JavaScript

If I have a symlink pointing to a path outside the app folder, e.g.

JavaScript

The FastAPI application can recognize the URL xyz.com/public/index.html, but it can’t recognize xyz.com/public/data.

Is this doable? Unfortunately, I cannot use FileResponse due to the blob size being too large. I want to return the file with a simple link somehow.

Advertisement

Answer

It is doable, as long as you mount a StaticFiles instance on that specific path as well. For example:

JavaScript

Then in your Jinja2 template you can requesst the files as below:

JavaScript

or, as per your given example (if there is a "static" directory including a “whatever.tgz” file):

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement