R
Railway12mo ago
RyanJI

Getting and downloading files from a Volume in Django

Hi I have a Django app deployed on Railway with a volume attached '/storage' which contains some sub-directories (invoices, news_images, driver_images) which each contain images or pdf's. I have a 'file viewer' page which displays these files to be downloaded.
volume_path = settings.MEDIA_ROOT
def ensure_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)

ensure_dir(os.path.join(volume_path, "driver_images"))
ensure_dir(os.path.join(volume_path, "news"))
ensure_dir(os.path.join(volume_path, "invoices"))

# Now list the directories
driver_images = os.listdir(os.path.join(volume_path, "driver_images"))
news_images = os.listdir(os.path.join(volume_path, "news"))
invoices_path = os.path.join(volume_path, "invoices")
volume_path = settings.MEDIA_ROOT
def ensure_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)

ensure_dir(os.path.join(volume_path, "driver_images"))
ensure_dir(os.path.join(volume_path, "news"))
ensure_dir(os.path.join(volume_path, "invoices"))

# Now list the directories
driver_images = os.listdir(os.path.join(volume_path, "driver_images"))
news_images = os.listdir(os.path.join(volume_path, "news"))
invoices_path = os.path.join(volume_path, "invoices")
I'm ensuring the directory, and the funny thing is the files are clearly there as the path was found, however images don't load and pdf's aren't accessible. I have removed trailing slashes as Django adds slashes to urls by default which I thought could be the issue
function removeTrailingSlash(uri) {
if (uri.endsWith('/')) {
return uri.slice(0, -1);
}
return uri;
}

// Remove trailing slashes from file URIs
fileUris.forEach(function(uri) {
uri.href = removeTrailingSlash(uri.href);
});

// Remove trailing slashes from pdf URIs
pdfUris.forEach(function(uri) {
uri.href = removeTrailingSlash(uri.href);
});
});
function removeTrailingSlash(uri) {
if (uri.endsWith('/')) {
return uri.slice(0, -1);
}
return uri;
}

// Remove trailing slashes from file URIs
fileUris.forEach(function(uri) {
uri.href = removeTrailingSlash(uri.href);
});

// Remove trailing slashes from pdf URIs
pdfUris.forEach(function(uri) {
uri.href = removeTrailingSlash(uri.href);
});
});
However this hasn't solved it. This works in the local environment, just not in the railway environment. Is there some strange quirk about the volumes in Railway? Not having FTP or SSH is the reason we need this, however any workaround will do. The page renders with the image filenames and pdf's listed, however they lead to 404s When inspecting in the F12 menu, the URI's for the images are what should be correct, however also lead to 404s. Thanks
No description
No description
No description
3 Replies
Brody
Brody12mo ago
your volume is mounted to /storage but it sounds like you are trying to save and load files into a storage folder that is relative to your project folder. when you mount a volume to your service it is mounted at the root of the container, not at the root of your project, you may instead want to mount the volume to /app/storage as /app is where your project is copied into
RyanJI
RyanJIOP12mo ago
Hi Brody thanks for your help. I have changed the mount path to /app/storage/, changed the media root to correspond with that. Still with the same issue of the files being discoverable, but not downloadable. Thanks Edit : For more context, it works in the testing environment, but not the production environment. Same code, same setup with the volume etc.
Brody
Brody12mo ago
have a look at how this template handles serving the files https://github.com/vfehring/django-volumes specifically the urls.py file
Want results from more Discord servers?
Add your server