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.



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

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

Instance Method Details

#download(s3_location, local_path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/s3sync/syncer.rb', line 37

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

  @log.info "Downloading"
  
  local_files = local_files(local_path)
  remote_files(bucket_name, folders) do |s3|
    next if FileDiff::same_file? s3, local_files[s3[:key]]
    begin
      destination_file = File.join destination_folder, s3[:key]
      @log.info "#{s3[:file].public_url} => #{destination_file}"
      s3_download s3, destination_file
    rescue Exception => e
      @log.error e
    end
  end
  @log.info "Done"
rescue Exception => e
  @log.error e
end

#upload(local_path, s3_url) ⇒ Object



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

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

  @log.info "Uploading files" 

  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|
    begin
      s3_key = File.join folders, key
      @log.info "#{item[:file]} => s3://#{bucket_name}/#{s3_key}"
      s3_upload item, bucket_name, s3_key
    rescue Exception => e
      @log.error e
    end
  end
  @log.info "Done"
rescue Exception => e
  @log.error e
end