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
Project ID:
N/A
NA
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 volumeso, 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
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
folderso,what to write in env?
You don't. You do not need to create that env variable. Railway injects it into your container for you.
oh ok
please let us know when you have finished reading this page
https://docs.railway.app/reference/volumes
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 aheadwhat mount point is your volume currently set to? please show a screenshot
i have added the same in variables:
DO NOT ADD THEM IN VARIABLES
They are automatically injected to your project on deploy.
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 workSorry to ask this again,
But my path to upload images is public/uploads
okay please mount it to the path I have specified
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 thisBecause 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}
My frontend is hosted in Vercel, wont the env fail?
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
ok
we are not using any library to handle file uploads in backend
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