Class: S3_Multi_Upload::Upload
- Inherits:
-
Object
- Object
- S3_Multi_Upload::Upload
- Defined in:
- lib/s3_multi_upload.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#file ⇒ Object
Returns the value of attribute file.
-
#mutex ⇒ Object
Returns the value of attribute mutex.
-
#object ⇒ Object
Returns the value of attribute object.
-
#options ⇒ Object
Returns the value of attribute options.
-
#progress ⇒ Object
Returns the value of attribute progress.
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#s3 ⇒ Object
Returns the value of attribute s3.
Instance Method Summary collapse
- #chunk_size ⇒ Object
- #enqueue ⇒ Object
-
#initialize(options) ⇒ Upload
constructor
A new instance of Upload.
- #normalize(value, unit = nil) ⇒ Object
- #process ⇒ Object
- #upload ⇒ Object
Constructor Details
#initialize(options) ⇒ Upload
Returns a new instance of Upload.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/s3_multi_upload.rb', line 10 def initialize AWS.config :access_key_id => [:access_key_id], :secret_access_key => [:secret_access_key] @options = @file = Pathname.new [:file] @queue = Queue.new @mutex = Mutex.new @s3 = AWS::S3.new @bucket = @s3.buckets.create [:bucket] @object = @bucket.objects[[:key] || @file.basename] enqueue end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
8 9 10 |
# File 'lib/s3_multi_upload.rb', line 8 def bucket @bucket end |
#file ⇒ Object
Returns the value of attribute file.
8 9 10 |
# File 'lib/s3_multi_upload.rb', line 8 def file @file end |
#mutex ⇒ Object
Returns the value of attribute mutex.
8 9 10 |
# File 'lib/s3_multi_upload.rb', line 8 def mutex @mutex end |
#object ⇒ Object
Returns the value of attribute object.
8 9 10 |
# File 'lib/s3_multi_upload.rb', line 8 def object @object end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/s3_multi_upload.rb', line 8 def @options end |
#progress ⇒ Object
Returns the value of attribute progress.
8 9 10 |
# File 'lib/s3_multi_upload.rb', line 8 def progress @progress end |
#queue ⇒ Object
Returns the value of attribute queue.
8 9 10 |
# File 'lib/s3_multi_upload.rb', line 8 def queue @queue end |
#s3 ⇒ Object
Returns the value of attribute s3.
8 9 10 |
# File 'lib/s3_multi_upload.rb', line 8 def s3 @s3 end |
Instance Method Details
#chunk_size ⇒ Object
40 41 42 |
# File 'lib/s3_multi_upload.rb', line 40 def chunk_size normalize *[:chunk_size].first end |
#enqueue ⇒ Object
44 45 46 47 48 |
# File 'lib/s3_multi_upload.rb', line 44 def enqueue (file.size.to_f / chunk_size).ceil.times do |index| queue << [chunk_size * index, index + 1] end end |
#normalize(value, unit = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/s3_multi_upload.rb', line 27 def normalize value, unit = nil case unit.downcase.to_sym when nil, :b, :byte, :bytes value.to_f when :k, :kb, :kilobyte, :kilobytes value.to_f * 1024 when :m, :mb, :megabyte, :megabytes value.to_f * 1024 ** 2 when :g, :gb, :gigabyte, :gigabytes value.to_f * 1024 ** 3 end end |
#process ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/s3_multi_upload.rb', line 88 def process value, unit = *[:chunk_size].first puts "uploading #{file} to s3://#{[:bucket]}/#{object.key} using #{[:threads]} threads in chunks of #{value} #{unit}" progress if [:progress_bar] abort 'upload failed' unless upload progress.finish if [:progress_bar] end |
#upload ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/s3_multi_upload.rb', line 50 def upload object.multipart_upload do |upload| [:threads].times.collect do Thread.new do until queue.empty? offset, index = queue.deq :asynchronously rescue nil unless offset.nil? upload_parameters = { :data => file.read(chunk_size, offset), :part_number => index, } if [:checksum] digest = Digest::MD5.digest(upload_parameters[:data]) encoded_digest = Base64.encode64(digest).strip upload_parameters[:content_md5] = encoded_digest end upload.add_part upload_parameters if [:progress_bar] mutex.synchronize do progress.inc end end end end end end.each(&:join) end end |