R
Railway13mo ago
Dheeraj

How to work with volume?

I added a volume to my service but I dont know how to use it. I want to upload images in that volumne via my frontend which usually goes to a path named public/uploads. How can i use volumne?
24 Replies
Percy
Percy13mo ago
Project ID: N/A
Dheeraj
DheerajOP13mo ago
NA
MantisInABox
MantisInABox13mo ago
When you create your volume, you set a mount path, such as /app/public/uploads <- your application lives inside the /app folder in the container. Then, in your code, anytime you upload a file, or retrieve a file, you would use the environment variable RAILWAY_VOLUME_MOUNT_PATH instead of hardcoding or setting another location. This will point directly to the mount path that you set when creating the volume
Dheeraj
DheerajOP13mo ago
so, if I have uploads folder in public/uploads i have to mount my volume in /public/uploads and in the code: in my env files RAILWAY_VOLUME_MOUNT_PATH : (to be set to what?) and use this as folder
MantisInABox
MantisInABox13mo ago
You don't have to set RAILWAY_VOLUME_MOUNT_PATH, it is set by default for you. If you are using node, you would use process.env.RAILWAY_VOLUME_MOUNT_PATH in your code and it will point to your public/uploads folder
Dheeraj
DheerajOP13mo ago
so,what to write in env?
No description
Dheeraj
DheerajOP13mo ago
No description
MantisInABox
MantisInABox13mo ago
You don't. You do not need to create that env variable. Railway injects it into your container for you.
Dheeraj
DheerajOP13mo ago
oh ok
Brody
Brody13mo ago
please let us know when you have finished reading this page https://docs.railway.app/reference/volumes
Dheeraj
DheerajOP13mo ago
so, its like this: file.mv(${process.env.RAILWAY_VOLUME_MOUNT_PATH}/${fileName}) and the env will be taken from the variables, correct? i have read it, but was not getting it, how to proceed ahead
Brody
Brody13mo ago
what mount point is your volume currently set to? please show a screenshot
Dheeraj
DheerajOP13mo ago
No description
Dheeraj
DheerajOP13mo ago
i have added the same in variables:
No description
MantisInABox
MantisInABox13mo ago
DO NOT ADD THEM IN VARIABLES They are automatically injected to your project on deploy.
Brody
Brody13mo ago
mount it to /app/storage and as vin has said three times, do not set them yourself then once you mount to the mount point I gave, I think this code will work
Dheeraj
DheerajOP13mo ago
Sorry to ask this again, But my path to upload images is public/uploads
Brody
Brody13mo ago
okay please mount it to the path I have specified
Dheeraj
DheerajOP13mo ago
ok i added the changes for mouting, and added the changes in code for that env old: file.mv(./public/uploads/${fileName}) new: file.mv(${process.env.RAILWAY_VOLUME_MOUNT_PATH}/${fileName}) but its not working, now the file itself is not showing when uploaded Sorry to ask such basic questions but I am new to this
MantisInABox
MantisInABox13mo ago
Because now they should be uploaded to your volume... upload a bunch of files and see if the storage usage increases. Then you need to update where you read the file to use the same ${process.env.RAILWAY_VOLUME_MOUNT_PATH}/${fileName}
Dheeraj
DheerajOP13mo ago
My frontend is hosted in Vercel, wont the env fail?
Brody
Brody13mo ago
writing to the volume has to be done by the railway service the volume is attached to what libraries are you using to handle file uploads on the backend
Dheeraj
DheerajOP13mo ago
ok we are not using any library to handle file uploads in backend
Brody
Brody13mo ago
I'm sorry to say this but, this help thread is turning into more of a programming help thread, and unfortunately that's not what these threads are intended for, we wish you all the best while you try to solve this issue
Want results from more Discord servers?
Add your server