public
Last active

Ruby Net:HTTP chunked transfer

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
require 'uri'
require 'net/http'
 
class Chunked
def initialize(data, chunk_size)
@size = chunk_size
if data.respond_to? :read
@file = data
end
end
def read(foo)
if @file
@file.read(@size)
end
end
def eof!
@file.eof!
end
def eof?
@file.eof?
end
end
 
parsed = URI::parse(@storage_url)
conn = Net::HTTP.new(parsed.host, parsed.port)
 
fp = File::open('test.txt')
parsed.path += '/foo/test.txt'
 
chunked = Chunked.new(fp, 5)
 
request = Net::HTTP::Put.new parsed.request_uri, {'x-auth-token' => @auth_token, 'Transfer-Encoding' => 'chunked', 'content-type' => 'text/plain'}
request.body_stream = chunked
conn.start do |http|
http.request(request)
end
 
fp.close

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.