1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #1852 from rackspace/example_fix

[rackspace|storage] fixing large upload example and documentation
This commit is contained in:
Kyle Rames 2013-06-05 07:00:38 -07:00
commit 31c921eb5b
2 changed files with 37 additions and 32 deletions

View file

@ -375,27 +375,28 @@ Cloud Files requires files larger than 5 GB to be uploaded into segments along w
SEGMENT_LIMIT = 5368709119.0 # 5GB -1 SEGMENT_LIMIT = 5368709119.0 # 5GB -1
BUFFER_SIZE = 1024 * 1024 # 1MB BUFFER_SIZE = 1024 * 1024 # 1MB
File.open("large_file") do |f| File.open(file_name) do |f|
num_segments = (f.size / SEGMENT_LIMIT).round + 1 segment = 0
1.upto(num_segments) do |segment| until file.eof?
segment += 1
offset = 0 offset = 0
read = 0
# upload segment to cloud files # upload segment to cloud files
service.put_object("my_container", "large_file/#{segment}", nil, options = {}) do segment_suffix = segment.to_s.rjust(10, '0')
if (offset < SEGMENT_LIMIT) && (read.zero? || read == BUFFER_SIZE) service.put_object("my_container", "large_file/#{segment_suffix}", nil) do
buf = f.sysread(BUFFER_SIZE) if offset <= SEGMENT_LIMIT - BUFFER_SIZE
read = buf.size buf = file.read(BUFFER_SIZE).to_s
offset += read offset += buf.size
buf buf
else else
"" ''
end end
end end
end end
end end
# write manifest file # write manifest file
service.put_object_manifest("my_container", "large_file") service.put_object_manifest("my_container", "large_file", 'X-Object-Manifest' => "my_container/large_file/")
Segmented files are downloaded like ordinary files. See [Download Files](#download-files) section for more information. Segmented files are downloaded like ordinary files. See [Download Files](#download-files) section for more information.

View file

@ -61,38 +61,42 @@ file_name = get_user_input "Enter full path of file to upload"
segment_name = File.basename(file_name) segment_name = File.basename(file_name)
File.open(file_name) do |f| File.open(file_name) do |f|
num_segments = (f.size / SEGMENT_LIMIT).round + 1 num_segments = (f.stat.size / SEGMENT_LIMIT).round + 1
puts "\nThis upload of '#{file_name}' will require #{num_segments} segment(s) and 1 manifest file\n" puts "\nThis upload of '#{file_name}' will require #{num_segments} segment(s) and 1 manifest file\n"
1.upto(num_segments) do |segment|
print "\n\tUploading segment #{segment} " segment = 0
offset = 0 until f.eof?
read = 0 segment += 1
service.put_object(directory.key, "#{segment_name}/#{segment}", nil, options = {}) do offset = 0
if (offset < SEGMENT_LIMIT) && (read.zero? || read == BUFFER_SIZE)
print "." # upload segment to cloud files
buf = f.sysread(BUFFER_SIZE) segment_suffix = segment.to_s.rjust(10, '0')
read = buf.size print "\n\tUploading segment #{segment_suffix} "
offset += read service.put_object(directory.key, "#{segment_name}/#{segment_suffix}", nil) do
buf if offset <= SEGMENT_LIMIT - BUFFER_SIZE
else print "."
"" buf = f.read(BUFFER_SIZE).to_s
end offset += buf.size
end buf
end else
end ''
end
end
end
end
puts "\n\n\tWriting manifest #{segment_name}\n\n" puts "\n\n\tWriting manifest #{segment_name}\n\n"
service.put_object_manifest(directory.key, segment_name) service.put_object_manifest(directory.key, segment_name, 'X-Object-Manifest' => "#{directory.key}/#{segment_name}/" )
puts <<-NOTE puts <<-NOTE
You should now be able to download #{segment_name} from the cloud control panel or using the following code: You should now be able to download #{segment_name} from the cloud control panel or using the following code:
directory = service.directories.get('#{directory.key}') directory = service.directories.get('#{directory.key}')
File.open('downloaded_#{segment_name}', 'w') do | f | File.open('downloaded_#{segment_name}', 'w') do | f |
directory.files.get(#{segment_name}) do | data, remaining, content_length | directory.files.get('#{segment_name}') do | data, remaining, content_length |
print "." print "."
f.syswrite data f.write data
end end
end end