#Getting and downloading files from a Volume in Django

4 messages · Page 1 of 1 (latest)

fervent ore
#

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")

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);
    });
  });

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

cerulean merlinBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

empty charm
#

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

fervent ore