Class: Hobo::Lib::S3::Sync
- Inherits:
-
Object
- Object
- Hobo::Lib::S3::Sync
- Includes:
- Hobo::Logging
- Defined in:
- lib/hobo/lib/s3/sync.rb
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Sync
constructor
A new instance of Sync.
- #sync(source, dest, opts = {}) ⇒ Object
Methods included from Hobo::Logging
Constructor Details
#initialize(opts = {}) ⇒ Sync
Returns a new instance of Sync.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/hobo/lib/s3/sync.rb', line 7 def initialize opts = {} require 'aws-sdk' @opts = { :access_key_id => nil, :secret_access_key => nil, :verify_response_body_content_length => false, :max_retries => 15 }.merge(opts) handle_s3_error do # AWS::S3 is flakey about actually raising this error when nil is provided [:access_key_id, :secret_access_key].each do |k| raise AWS::Errors::MissingCredentialsError if @opts[k].nil? end end logger.debug("s3sync: Options #{@opts}") end |
Instance Method Details
#sync(source, dest, opts = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 |
# File 'lib/hobo/lib/s3/sync.rb', line 27 def sync source, dest, opts = {} delta = {:add => [], :remove => []} handle_s3_error do opts = { :delete => true, :dry => false, :progress => Hobo.method(:progress) }.merge(opts) source_io = io_handler(source) destination_io = io_handler(dest) logger.debug("s3sync: Synchronzing (#{source_io.class.name} -> #{destination_io.class.name}") raise "S3 -> S3 synchronisation not supported" if source_io.is_a? Remote and destination_io.is_a? Remote source_listing = source_io.ls destination_listing = destination_io.ls logger.debug("s3sync: Source listing - #{source_listing}") logger.debug("s3sync: Destination listing - #{destination_listing}") delta = delta(source_listing, destination_listing) logger.debug("s3sync: Delta #{delta}") break if opts[:dry] delta[:add].each do |file| logger.debug("s3sync: Synchronizing #{file}") source_file = source_io.open(file, "r") destination_file = destination_io.open(file, "wb+") source_file.buffer size = source_file.size destination_file.write({ :size => source_file.size }) do |buffer, bytes| chunk = source_file.read(bytes) buffer.write(chunk) opts[:progress].call(file, (chunk || '').length, size, :update) end destination_file.close source_file.close opts[:progress].call(file, 0, size, :finish) end break unless opts[:delete] delta[:remove].each do |file| logger.debug("s3sync: Removing #{file}") destination_io.rm(file) end end return delta end |