2 years ago
I’m working on setting up volumes for my Sveltekit project and I’m stuck. I’ve created a GitHub repo with the exact code I am using to make it easier to spot the issue: https://github.com/JaviKeziah/volumes-test
I'm not adding the RAILWAYVOLUMEMOUNT_PATH in production, just like the docs suggest.
The RAILWAYVOLUMEMOUNT_PATH is /app/uploads. I've tried /uploads too with the same result.
Could you take a look and let me know what I’m missing?
12 Replies
2 years ago
I am not getting any errors. The images are saved successfully, but I don't know where XD.
But I know they are not saved in the volume because I can not access to the images by the path I've previously set /uploads/.... And I've uploaded a lot of images but I see no activity in the volume's metric.
2 years ago
Here is the endpoint where I am saving the images with Node:
import { NODE_ENV, RAILWAY_VOLUME_MOUNT_PATH } from '$env/static/private';
import { json, type RequestHandler } from '@sveltejs/kit';
import fs from 'fs';
import path from 'path';
export const POST: RequestHandler = async ({ request }) => {
const formData = await request.formData();
const file = formData.get('file') as File;
const uploadDir = RAILWAY_VOLUME_MOUNT_PATH;
// !save file locally
const fileName = `primera-img${path.extname(file.name)}`;
// const uploadPath = RAILWAY_VOLUME_MOUNT_PATH + fileName;
const uploadPath =
NODE_ENV === 'development'
? path.join(uploadDir, fileName)
: `${RAILWAY_VOLUME_MOUNT_PATH}/${fileName}`;
try {
// Create the upload directory if it doesn't exist
await fs.promises.mkdir(uploadDir, { recursive: true });
const arrayBuffer = await file.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
await fs.promises.writeFile(uploadPath, buffer);
console.log('Archivo guardado exitosamente');
} catch (err) {
console.error('Error al guardar el archivo:', err);
return json({ success: false, msg: 'Error al guardar el archivo' });
}
return json({ success: true });
};2 years ago
I have a sneaking suspicion that you're reading the RAILWAY_VOLUME_MOUNT_PATH incorrectly, can you debug log that variable please.
2 years ago
Done. The value is /app/uploads
2 years ago
So, the path were the image is saved is : /app/uploads/primera-img.jpeg. But there is nothing there when I put that path inside the src of an image.
2 years ago
You need to handle serving the file from the volume too, that's not something Railway does for you.
2 years ago
Yes, I know, but according to the docs I should be able to access to the file using the absolute path I've set when creating it, and I am not: " your application will be able to access it at the absolute path /foobar." . Have you tried creating a volume for a SvelteKit project before?
2 years ago
I have not used SvelteKit before, but I've used a volume many times with node without issues.
2 years ago
according to the docs I should be able to access to the file using the absolute path I've set when creating it
Yes you can, as a file path, not as a file server, volumes do not come with file servers.
In +Page.svelte you are using a file path in a img element's src attribute, but I don't see any code to serve the files from the disk at that path.
Status changed to Solved Railway • almost 2 years ago
2 years ago
Aah, ok. I didn't know that. As I've tried again by retrieving the img from my server too and didn't work, I've finally tried another service. As a suggestion: If Railway would offer a service like Cloudlfare R2 or S3 to save files and serve them from your own domain, it would be awesome.
2 years ago
You can already deploy MinIO (a self hosted S3 compatible service)
But of course serving files directly from a volume also works just fine, you only need to write the code to do that.