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 = :b) ⇒ 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 26 27 28 29 30 |
# 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 = case [:create_bucket] when true @s3.buckets.create [:bucket] else @s3.buckets[[:bucket]] end @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
45 46 47 |
# File 'lib/s3_multi_upload.rb', line 45 def chunk_size normalize *[:chunk_size].first end |
#enqueue ⇒ Object
49 50 51 52 53 |
# File 'lib/s3_multi_upload.rb', line 49 def enqueue (file.size.to_f / chunk_size).ceil.times do |index| queue << [chunk_size * index, index + 1] end end |
#normalize(value, unit = :b) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/s3_multi_upload.rb', line 32 def normalize value, unit = :b case unit.downcase.to_sym when :b, :byte, :bytes value.to_f when :k, :kb, :kilobyte, :kilobytes value.to_f * (2 ** 10) when :m, :mb, :megabyte, :megabytes value.to_f * (2 ** 20) when :g, :gb, :gigabyte, :gigabytes value.to_f * (3 ** 30) end end |
#process ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/s3_multi_upload.rb', line 93 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
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 83 84 85 86 87 |
# File 'lib/s3_multi_upload.rb', line 55 def upload object.multipart_upload do |upload| [:threads].times.map 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 |