Class: S3sync::Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/s3sync/syncer.rb

Instance Method Summary collapse

Constructor Details

#initializeSyncer

Returns a new instance of Syncer.



8
9
10
11
# File 'lib/s3sync/syncer.rb', line 8

def initialize
  @s3 = AWS::S3.new
  @log = Syslog::Logger.new 'S3sync'
end

Instance Method Details

#download(s3_location, local_path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/s3sync/syncer.rb', line 34

def download(s3_location, local_path)
  bucket_name, *folders = s3url_to_bucket_folder s3_location
  destination_folder = File.absolute_path(local_path)

  log "Downloading"
  # Yielding the remote s3 files and doing a 2 pass filter
  # as better performance than computing a diff of 2 complete directory listings
  local_files = local_files(local_path)
  remote_files(bucket_name, folders) do |s3|
    next if FileDiff::same_file? s3, local_files[s3[:key]]
    destination_file = File.join destination_folder, s3[:key]
    log "#{s3[:file].public_url} => #{destination_file}"
    s3_download s3, destination_file
  end
  log "done"
end

#upload(local_path, s3_url) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/s3sync/syncer.rb', line 13

def upload(local_path, s3_url)
  bucket_name, *folders = s3url_to_bucket_folder s3_url

  log "Uploading files" 
  # Yielding the remote s3 files and doing a 2 pass filter
  # as better performance than computing a diff of 2 complete directory listings
  local_files = local_files(local_path)
  remote_files(bucket_name, folders) do |s3|
    source = local_files[s3[:key]]
    local_files.delete s3[:key] if FileDiff::same_file? source, s3
  end

  local_files.each do |key,item|
    s3_key = File.join folders, key
    log "#{item[:file]} => s3://#{bucket_name}/#{s3_key}"
    s3_upload item, bucket_name, s3_key
  end
  log "done"

end