Django + Gunicorn media files

Running Django + Gunicorn, I’ve switched to DEBUG=False and realized I’m going to need a way to host my static and media files. The static files were fine, I use:

[[statics]]
  guest_path = "/app/static"
  url_prefix = "/static/"

But media is dynamic, therefore I can’t as they should be present in the image. I’ve create a volume which is mounted via:

[mounts]
  destination = "/data"
  source = "data"

In a traditional setup I would use Nginx to serve these files. Because Fly.io is acting as my Nginx in this case, is there a way to do this via the UI? Is there a way to easily serve media files without needing another server? There seems to be a lack of documentation for what should be a core part of using Django on Fly.io! How has everyone else got around this? Ideally I keep all my infrastructure on site, without needing an S3 bucket or seperate hosting provider.

Use WhiteNoise. There should be more docs on Django + Fly shortly.

WhiteNoise doesn’t solve the media files aspect to this, does it? I would have thought the answer is that you have to use something like S3 as described here: Storing Django Static and Media Files on Amazon S3 | TestDriven.io

I am very interested in learning about how to handle media files on fly.io w/out the use of AWS S3. Thanks!:slight_smile:

Correct @czue, WhiteNoise is only for static files. You’d normally use django-storages for media files and can host anywhere though S3 is commonly used.

1 Like

Off the top of my head I “think” you could just use a Volume though I need to play around with this to confirm and make sure no security issues.

@wsvincent that works for the upload, but the volume isn’t accessible to the outside world.

Just for reference, I ended up running a nginx process in the background which exposes the volume. This isn’t ideal!

Oh, interesting. Hmm… makes sense around exposure. Honestly django-storages plus S3/Netlify/whatever works so well that I’m not sure how deep down this rabbit hole I’m motivated to go personally. Though I am curious technically what’s possible! Maybe someone from Fly will weigh in around exposing volumes in a safe way :man_shrugging:

1 Like

Hi there.

I found a solution for this issue.
First, I created a volume for each machine, used the same volume name, did it according to the docs on Fly.io.
Then I mounted the volume in my toml file, to make sure my media folder points to the volume, like this:

[mounts]
  source = "media_data"
  destination = "/code/media"

After that I just experimented a bit by adding the following code to my toml file as inspired by reading Fly documentation (there was already a similar section for statics, I just added one more):

[[statics]]
  guest_path = '/code/media'
  url_prefix = '/media/'

And it works pretty well! Now all images uploaded via admin interface are safely stored on the volume, and they are displayed by my website as well.

Not sure whether this is 100% safe usage of volumes so no guarantees.