Where can you store Media Files?
I'm using Django and have added a blog module to it where users can upload their own images. I was wondering whether it is possible to store those images on Railway? Thanks!
12 Replies
Project ID:
N/A
N/A
Hello, you'll want to use a volume to store persistent data. https://docs.railway.app/reference/volumes
Hi, thanks for letting me know. I added a volume to my web app but wasn't able to get it to work.
It's linked to the project but nothing seems to get added to it despite the URLs pointing to /media/ as well
please reference this template
https://railway.app/template/AWUIv6
I took a look at the template and implemented its suggestions which seemed to be changing the media root to
MEDIA_ROOT = os.environ["RAILWAY_VOLUME_MOUNT_PATH"]
and then the volume’s Mount Path to /app/media
.
After doing that, I can now see that the files are uploading somewhere as the disk usage is increasing. The problem is the files are still not showing up on Wagtail Admin (the django blog app) and the image links are not working.
Any suggestions on what to try next? I’m pretty much stumped.Are the links pointing to the correct location? Maybe you also have to configure wagtail to work with the new path.
Do you know if there's any way to access the volume manually through the command line?
There is not that I know of, but you could probably use this. https://railway.app/template/Nan7Bs There is another one for downloading.
I finally resolved this by reviewing all the code in the django-volumes example. The guy who wrote this wasn't really clear on what changes to make so it took ages to figure out. Also, he seems to be using an unrecommended workaround which isn't meant for production.
It was this line that ultimately got it working:
urlpatterns += [ re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})]
This is where in the documentation it clearly states that this is for local development only: https://docs.djangoproject.com/en/4.2/ref/views/#django.views.static.serve
Is it better to just split user-uploaded files off and serve them from an S3 bucket?yes that isn't recommended for production but as that template is a starter project it is perfectly adequate for its intended usecase, and as your project grows and you learn more about web development you can implement a static file server with caddy or nginx yourself