R
Railway12mo ago
Sami

Object storage service

whoever read this I hope you are having great time 🌹 , I need help with understanding volumes. Does railway have a solution for Object storage, i mean something alternative to "Amazon S3" or "DigitalOcean Spaces". I would appreciate if one could explain the difference between such services and "Railway Volumes". I'm not sure I understood what volumes are and how to utilize them correctly.
58 Replies
Percy
Percy12mo ago
Project ID: N/A
Sami
Sami12mo ago
N/A
Brody
Brody12mo ago
you can think of volumes like a mounted drive on your computer, because that's pretty much exactly what they are. a persistent storage volume mounted to your chosen mount point that provides filesystem storage. and railway has a minio template which uses the volumes for the underlying storage and exposes an S3 compatible api https://railway.app/template/SMKOEA
Sami
Sami12mo ago
But how can i access that drive (volume) from my project files and during development?
Brody
Brody12mo ago
the same way you would access any files on disk, if you mount the volume to /data for example, then you save a file like /data/text.txt and then read it back from that path
Sami
Sami12mo ago
I'll test it out ok 👍
Brody
Brody12mo ago
for sure, if you run into troubles you can come ask
Sami
Sami12mo ago
Many thanks man 🌹
Brody
Brody12mo ago
my pleasure
Sami
Sami12mo ago
I'm currently testing it with a DRF (Django Rest Framework) app to upload files. Locally, everything works perfectly fine, but I encountered a problem when I deployed it. I am using Dockerfile. The problem specifically arises when I try to access the uploaded files on Railway.app. On my localhost, when I click on the file path, the file downloads without any issues. However, on Railway.app, it gives me a 'Page Not Found' response instead of the file. To provide some context, in my Dockerfile, I have set the working directory, and the files are saved in a folder called 'media' within the project root. The API is receiving (post request) with no issues, and because the file is stored in the project root; it can retrieves data such as file name and file size through GET request. These attributes are not stored in the database. Instead, they are dynamically read from the saved file's attributes.
Sami
Sami12mo ago
On my localhost
Sami
Sami12mo ago
When deployed
Sami
Sami12mo ago
He only difference would be in the file path
Brody
Brody12mo ago
are you saving the files into the volume when on railway? it doesnt seem like you are doing that?
Sami
Sami12mo ago
Sami
Sami12mo ago
Sami
Sami12mo ago
I'll be honest with you i can't confirm if i am doing so or not. In fact that's what i would like to know. Normally that destination of the uploaded files should be "BASE_DIR/media"
Brody
Brody12mo ago
your media root should only be the volume mount point at least when running on railway
Sami
Sami12mo ago
Sami
Sami12mo ago
If the working dir is ./code Then what would be the path for railway volume? ./code/media? Or ./media ?
Brody
Brody12mo ago
you mounted the volume to /media so the volumes path would be /media
Sami
Sami12mo ago
So i should think of the project now as - media - code - - app . . . ?
Brody
Brody12mo ago
where does app come from
Sami
Sami12mo ago
(App is what ever code that is inside the ./code) Which is in this case:
Sami
Sami12mo ago
Brody
Brody12mo ago
correct
Sami
Sami12mo ago
Ok, i kept asking to make sure i understood you correctly. Thanks for being patient ❤️ I'll test it 👍 ------------------------------------------------- i Just had tried working it as you mentioned, changed the root of the mounted volume but still i cant reach the file for some reason. Pardon me for the late try, i couldn't play with it the last 2 days, if you noticed i was even sending screenshots from my phone and github mobile app.
Sami
Sami12mo ago
the updated root for the mount
Sami
Sami12mo ago
the structure is as: - code - - Dockerfile - - media - - core - - - settings.py dockerfile:
# Pull base image
FROM python:3.10.4-slim-bullseye

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# Copy project
COPY . .

RUN python manage.py collectstatic --noinput

# Run Server
CMD ["gunicorn", "core.wsgi:application"]
# Pull base image
FROM python:3.10.4-slim-bullseye

# Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# Copy project
COPY . .

RUN python manage.py collectstatic --noinput

# Run Server
CMD ["gunicorn", "core.wsgi:application"]
inside settings.py i set the following:
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"
Brody
Brody12mo ago
this is a classic case of not doing what I said lol ^
Sami
Sami12mo ago
Base_dir points to ./code Here it is ./code/media
Brody
Brody12mo ago
I know, you don't want that, the volume is mounted to the root on the container, not inside of the code folder ^
Sami
Sami12mo ago
No i changed that Look
Brody
Brody12mo ago
put it back to /media
Sami
Sami12mo ago
I tried that too It didn't work
Brody
Brody12mo ago
yes then remove the base dir
Sami
Sami12mo ago
I did yup
Brody
Brody12mo ago
okay and 404 right?
Sami
Sami12mo ago
Yup
Brody
Brody12mo ago
okay so then you have to find out a way to tell django to serve files from the media folder currently it expects a separate file server to do that for it
Sami
Sami12mo ago
I thing it has to do with django media and serving system and not railway volumes Yup I wished i didn't understood that currecrly and there is an easy way around it such as railway volumes
Brody
Brody12mo ago
show me your urls.py file please
Sami
Sami12mo ago
But i guess i have to do some configuration to the apache server or something to make this work 1 min
Brody
Brody12mo ago
yes this is type of thing django does expect of you, but that is too complex for a little site
Sami
Sami12mo ago
Brody
Brody12mo ago
django wants you to run it behind a proxy like nginx, where nginx will serve media files from the media volume, and proxy all remaining requests to django
Sami
Sami12mo ago
Yup thats what i found searching in the past 3 days But since i never did that before i tried looking for another away. Anyways i guess sooner or later i gotta learn how to integrate nginx with the stack 🤷‍♂️
Brody
Brody12mo ago
I'd personally use caddy but there has to be a way to get django to serve files from the volume too, may not be the best practice but I'm sure it can be done
Sami
Sami12mo ago
Honestly speaking i am still new to all this and there is already so many things to figure out like JWT auth between django and nuxt, learn nuxt itself, and then try doing something asynchronously
Brody
Brody12mo ago
I've never done jwt auth I've always just used uuid's in cookies
Sami
Sami12mo ago
The auth thing itself i am new to it 😂, just used to django-allauth without solid understanding, but since i want to move into building apis i thought of delving deeper into auth as well With apis and separated front-end, seriously the possibilities of one can make is endless 🔥. But the amount of topics to learn is also overwhelming too 😂
Brody
Brody12mo ago
slap an API key on your requests and you're good
Sami
Sami12mo ago
It is always refreshing talking to you man, thanks for your great efforts into this community. Btw what confuse me the most, is that the file is indeed uploaded and is there somehow, somewhere. Because the size, file name are dynamically read from file attr and not stored in the data base
Brody
Brody12mo ago
I'll see what I can whip up at a later date, so far this makes two people who have asked how to do this using a fileserver + proxy should also eliminate the need for whitenoise
Sami
Sami12mo ago
Yup, from my research it seem like if one used a cdn it will cover the static file and media files
Brody
Brody12mo ago
well yeah but then you aren't using volumes at all
Sami
Sami12mo ago
Yeah that will be an external service like digitalocean spaces or AWS S3.
Brody
Brody12mo ago
I don't know if I'd use object storage as a cdn