Created
July 15, 2013 21:37
-
-
Save krames/6003720 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'fog' | |
| SEGMENT_LIMIT = 1024 * 1024 * 10 | |
| BUFFER_SIZE = Excon.defaults[:chunk_size] || 1024 * 1024 | |
| Excon.defaults[:retry_limit] = 0 | |
| service = Fog::Storage.new({ | |
| :provider => 'Rackspace', | |
| :rackspace_region => :dfw | |
| }) | |
| # puts "directories>> #{service.directories}" | |
| fake_error = true | |
| File.open('/Users/kyle.rames/frank.jpg') do |file| | |
| segment = 0 | |
| until file.eof? | |
| segment += 1 | |
| # compute MD5 digest for the segment | |
| offset = 0 | |
| pos = file.pos | |
| md5 = Digest::MD5.new | |
| while offset <= SEGMENT_LIMIT - BUFFER_SIZE | |
| buf = file.read(BUFFER_SIZE) | |
| break unless buf | |
| offset += buf.size | |
| md5 << buf | |
| end | |
| file.seek(pos) | |
| # upload segment to cloud files | |
| offset = 0 | |
| begin | |
| etag = (segment == 2) && fake_error ? "bad" : md5.hexdigest | |
| options = {'ETag' => etag} | |
| puts "etag>> #{options.inspect}" | |
| service.put_object('bucket', "frank.jpg/#{segment.to_s.rjust(10, '0') }", nil, options) do | |
| print "." | |
| if offset <= SEGMENT_LIMIT - BUFFER_SIZE | |
| buf = file.read(BUFFER_SIZE).to_s # nil => '' | |
| offset += buf.size | |
| buf | |
| else | |
| '' | |
| end | |
| end | |
| rescue Fog::Storage::Rackspace::ServiceError => e | |
| puts e.inspect | |
| fake_error = false | |
| options = {} | |
| file.seek(pos) | |
| service.reload | |
| retry | |
| end | |
| print "\n" | |
| end | |
| end | |
| service.put_object_manifest('bucket', 'frank.jpg', 'X-Object-Manifest' => "bucket/large_file/" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment