could someone help me with volumes?
whitetown
PROOP

2 years ago

Hello,
could someone help me with volumes?
I used this code to upload the file to temp dir (it's working):
const tempDir = fs.mkdirSync(path.join(os.tmpdir(), String(new Date().getTime())), { recursive: true })

const result = []
for (const file of files) {
    const filePath = path.join(tempDir, file.originalname)
    fs.writeFileSync(filePath, file.buffer)
    result.push(filePath)
}

return result

now I added a volume.
the mount path initially was /files , i.e.
const tempDir = '/files'

then I have tried to change it to /app/files , i.e.
const tempDir = '/app/files'

no luck
it cannot create a file there

Solved

40 Replies

whitetown
PROOP

2 years ago

b799250d-dd7b-47bb-be65-0ae878afd304


brody
EMPLOYEE

2 years ago

quick question, if they are temp files, why would they need to be stored in a volume?


whitetown
PROOP

2 years ago

we decided do not process files immediately, since what we are doing takes a time, but add them to a queue. Also, if we keep the orifinal file, it helps us to repeat/restart our operation in case something failed


brody
EMPLOYEE

2 years ago

gotcha


brody
EMPLOYEE

2 years ago

what is your current volume's mount point


whitetown
PROOP

2 years ago

/app/files


brody
EMPLOYEE

2 years ago

dockerfile or nixpacks deployment?


whitetown
PROOP

2 years ago

nixpacks I guess. I added it via control panel


brody
EMPLOYEE

2 years ago

Do you have a Dockerfile in your repo?


whitetown
PROOP

2 years ago

NO


brody
EMPLOYEE

2 years ago

what is your current path you are saving files into in code


whitetown
PROOP

2 years ago

the current path is the temp folder (I cannot make volumes work and restored temp)


brody
EMPLOYEE

2 years ago

please inform me of the full file path currently in use by your code that saves the temp files


whitetown
PROOP

2 years ago

"/tmp/1716569487905/CVENCedricLionelEscher_28091981.pdf"
"/tmp/1716569599042/Ivan Gerasymchuk CV2.pdf"
"/tmp/1716569599042/KARPOLAN's Resume.pdf"


whitetown
PROOP

2 years ago

they are already deleted


brody
EMPLOYEE

2 years ago

but you have mounted the volume to /app/tmp


whitetown
PROOP

2 years ago

the path above is
fs.mkdirSync(path.join(os.tmpdir(), String(new Date().getTime())), { recursive: true })

when I replace it to /app/files - it did not wotk


whitetown
PROOP

2 years ago

I can change the path right now and redeploy, just do not know what path shall I use


whitetown
PROOP

2 years ago

1243618254119764000


brody
EMPLOYEE

2 years ago

You need to mount the volume to the same path you save the files to


brody
EMPLOYEE

2 years ago

you also have now given me 3 different mount points for your volume makeing increasingly hard for me to help you


whitetown
PROOP

2 years ago

sorry, I do not understand. I have tried different paths.
tell me please what Mount Path shall I define and then what path shall I use to copy files


whitetown
PROOP

2 years ago

Using the Volume
The volume mount point you specify will be available in your service as a directory to which you can read/write. If you mount a volume to /foobar, your application will be able to access it at the absolute path /foobar.
^^^^^^ tried to write to such a folder - did not work

Relative Paths
Nixpacks, the default buildpack used by Railway, puts your application files in an /app folder at the root of the container. If your application writes to a directory at a relative path, and you need to persist that data on the volume, your mount path should include the app path.

For example, if your application writes data to ./data, you should mount the volume to /app/data.

^^^^^^ tried to write to such a folder - did not work either


brody
EMPLOYEE

2 years ago

what do you mean by "such a folder"


whitetown
PROOP

2 years ago

in the documentation you have two examples:
/foobar
/app/data

I have tried
/files
/app/files


brody
EMPLOYEE

2 years ago

mount volume to /app/files and save files to /app/files


whitetown
PROOP

2 years ago

ok. I am deploying. will try in a few minutes


whitetown
PROOP

2 years ago

okay, it seems working now. removing all logging and will try one more time…


brody
EMPLOYEE

2 years ago

sounds good


whitetown
PROOP

2 years ago

I think that my mistake was that I had to create the folder /app/files
honestly, I was thinking that railway will do it


brody
EMPLOYEE

2 years ago

if you mount a volume to /app/files an /app/files folder will exist within your container


whitetown
PROOP

2 years ago

well, it did not..
I even tries to list files/folders in the code and the folder did not exist

const walk = async (dirPath) =>
    Promise.all(
        await readdir(dirPath, { withFileTypes: true }).then((entries) =>
            entries.map((entry) => {
                const childPath = join(dirPath, entry.name)
                return entry.isDirectory() ? walk(childPath) : childPath
            })
        )
    )

const files = async (req, res, next) => {
    try {
        const result = await walk('/app/files') // '/app'
        res.status(200).json(result)
    } catch (error) {
        console.log(error)
        next(error)

    }
}

brody
EMPLOYEE

2 years ago

I'm sorry but that means you have misconfigured something, if a folder path was not created when mounting a volume that would break everyone's application who use volumes


brody
EMPLOYEE

2 years ago

/app/files would of course be empty though


whitetown
PROOP

2 years ago

okay. I am not so experienced with raylway as you are 😉 so I will agree with you.


adivikramp
TRIAL

2 years ago

Hey! I have a microservice where I create 3 temporary files for processing that are stored in my project's root directory. Can I set my projects root directory path as the mount path somehow if I want to use the volumes provided by railway?


brody
EMPLOYEE

2 years ago

Nope, volumes are empty when created, so that would therefore create an empty project directory, you would need to store the files in a subfolder.

But you said they are temp files, I can't see you needing a volume for temp files, volumes are used for persistence.


adivikramp
TRIAL

2 years ago

During processing, these files are added to a queue and eventually uploaded to file.io, from where users can download them. Although these files are temporary, they need to persist for a short period until the processing and upload are complete.

If I create a subfolder in my root directory with the name temp_files, Should my mount path then be set to /temp_files if I use volumes?


brody
EMPLOYEE

2 years ago

> Although these files are temporary, they need to persist for a short period until the processing and upload are complete.

The files will persist for as long as that particular deployment is active, I don't see the need for a volume here.

> If I create a subfolder in my root directory with the name temp_files, Should my mount path then be set to /temp_files if I use volumes?

Nope, volume mount points are absolute paths and are not relative to the project location within the container, if you are using nixpacks the default project directory would be /app so your mount point would be /app/temp_files


adivikramp
TRIAL

2 years ago

Okay. Thanks for the help. Appreciate it.


Status changed to Solved brody over 1 year ago


Loading...