Using rest-client to download a file to disk without loading it all in memory first

后端 未结 2 1831
天命终不由人
天命终不由人 2021-02-08 12:25

I am using rest-client to download large page (around 1.5 GB in size). Retrieved value is stored in memory than saved into a file. As result my program crashes with failed

2条回答
  •  情书的邮戳
    2021-02-08 13:07

    My original answer promoted passing a block to RestClient::Request#execute but this only passed data to the block once full response is read. Thus rendering the exercise worthless. This is how to properly do it:

    File.open('/tmp/foo.iso', 'w') {|f|
        block = proc { |response|
          response.read_body do |chunk|
            puts "Working on response" 
            f.write chunk
          end
        }
        RestClient::Request.new(method: :get, url: 'http://mirror.pnl.gov/releases/xenial/ubuntu-16.04-server-amd64.iso', block_response: block).execute
    }
    

    It is from the related rest-client project issue.

    Note: redirection does not work in this mode as well you lose HTTP exit status, cookies, headers, etc. Hope this is gonna be fixed some day.

提交回复
热议问题