Fetching a file hosted on Filebrowser via Private Network
aalfath
PROOP

2 years ago

So I currently have a file that I am hosting using Filebrowser template.

The file has a download link that can be accessed via public URL without any problem.

But whenever I try to download the file from another service in the same project using internal private network, the file couldn't seem to be downloaded.

What I did was that I simply changed the public URL into the private one, like so:
http://heroncopier-download.railway.internal:3000/api/public/dl/xxxxx/xxxxx.zip

As you can see in my above example, I did already changed the https into http and I also added the port after the internal URL.

Does anyone have any clue on how to solve this?

Thanks!

11 Replies

aalfath
PROOP

2 years ago

a764e147-c28b-4388-85d2-547de92d2fd0


brody
EMPLOYEE

2 years ago

please provide any errors you are getting


aalfath
PROOP

2 years ago

Hi @Brody, nevermind. I got mixed up between the native fetch() function and the one that is provided by node-fetch npm package.
This post gave me the clue: https://stackoverflow.com/a/74611064


brody
EMPLOYEE

2 years ago

So you where able to successfully download the file over the private network?


aalfath
PROOP

2 years ago

Yes, it works really well.

In case anyone would like to use the same approach here's the code snippet for express/nodejs:

            const url = 'http://xxxxx.railway.internal:3000/api/public/dl/C_izechY/data/xxxx.zip'; // This is the URL of a file hosted on Railway internal network using Filebrowser template (make sure to share the file first and set long expiry date)
            const response = await fetch(url);

            if (!response.ok) {
                res.sendStatus(500);
                return;
            }

            // Set headers
            res.setHeader('Content-Type', response.headers.get('Content-Type'));
            res.setHeader('Content-Length', response.headers.get('Content-Length'));
            res.setHeader('Content-Disposition', 'attachment; filename=xxxx.zip'); // replace with your desired filename

            // Pipe the response stream directly to res
            response.body.pipe(res);

Above code lets your server fetches the file from other service in the same project via Railway's private network.
You can also install npm package such as express-rate-limit to prevent anyone from abusing the download endpoint (so not to let them waste your egress bills, just make sure to setup x-forwarded-for header config properly on the limiter)

Using this approach, the file can only be downloaded via endpoint that you can control.

It is not maybe state-of-the-art solution but it works for my use-case for now.


brody
EMPLOYEE

2 years ago

i might even go as far as to turn [http://xxxxx.railway.internal:3000/api/public/dl/C_izechY/data/xxxx.zip](http://xxxxx.railway.internal:3000/api/public/dl/C_izechY/data/xxxx.zip) into an environment variable like DOWNLOAD_URL just in case you need to change it then you wont have to edit code


aalfath
PROOP

2 years ago

oh true, that's a neat approach. I'll do that. so at least whenever i need to change something, I don't have to push any changes in the code


aalfath
PROOP

2 years ago

👍


brody
EMPLOYEE

2 years ago

exactly


aalfath
PROOP

2 years ago

Thank you Brody!


brody
EMPLOYEE

2 years ago

no problem!


Loading...