且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用rest-client将文件下载到磁盘,而无需先将其全部加载到内存中

更新时间:2023-02-14 23:28:40

另一种方法是使用raw_response.这将直接保存到文件中,通常在/tmp中.这样可以毫无问题地处理重定向. 请参见流式响应.这是他们的示例:

Another way is to use raw_response. This saves directly to a file, usually in /tmp. This handles redirects without a problem. See Streaming Responses. Here's their example:

>> raw = RestClient::Request.execute(
           method: :get,
           url: 'http://releases.ubuntu.com/16.04.2/ubuntu-16.04.2-desktop-amd64.iso',
           raw_response: true)
=> <RestClient::RawResponse @code=200, @file=#<Tempfile:/tmp/rest-client.20170522-5346-1pptjm1>, @request=<RestClient::Request @method="get", @url="http://releases.ubuntu.com/16.04.2/ubuntu-16.04.2-desktop-amd64.iso">>
>> raw.file.size
=> 1554186240
>> raw.file.path
=> "/tmp/rest-client.20170522-5346-1pptjm1"