Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stream file using curl from one server to another(limited server resources)

My API server has a very limited disk space(500MB) and memory(1GB). One of the API calls that it gets, is to receive a file. The consumer calls API and passes URL to be downloaded.

The "goal" of my server is to upload this file to Amazon S3. Unfortunately, I can't ask the consumer to upload the file directly to S3(part of requirements).

The problem is, sometimes those are huge files(10GB) and saving them to disk and then uploading it to S3 is not an option(500MB disk space limit).

My question is, how can I "pipe" the file from the input url to S3 using curl Linux program ?

Note: I was able to pipe it in different ways but, either it first tries to download the whole file and fails or I hit memory error and curl quits. My guess is that the download is much faster than the upload, so the pipe buffer/memory grows and explodes(1GB memory on server) when I get 10GB files.

Is there a way to achieve what I'm trying to do using curl and piping ?

Thank you, - Jack

like image 627
Joe Avatar asked Aug 10 '17 16:08

Joe


1 Answers

Another SO user asked a similar question about curl posts from stdin. See use pipe for curl data.

Once you are able to post your upload stream from the output of the first curl process's standard output, if you are running out of memory because you are downloading faster than you can upload, have a look at the mbuffer utility. I haven't used it myself, but it seems to be designed for exactly this sort of problem.

Lastly, if all else fails, I guess you could use curl's --limit-rate option to lock the transfer rates of the upload and download to some identical and sustainable values. This potentially underutilizes bandwidth, and won't scale well with multiple parallel download/upload streams, but for some one-off batch process, it might be good enough.

like image 140
terafl0ps Avatar answered Oct 14 '22 02:10

terafl0ps