Class: S3Archive::S3FileSynchronizer

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/s3archive/s3_file_synchronizer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(local_file, s3_file) ⇒ S3FileSynchronizer

Returns a new instance of S3FileSynchronizer.



10
11
12
13
# File 'lib/s3archive/s3_file_synchronizer.rb', line 10

def initialize(local_file, s3_file)
  @local_file = local_file
  @s3_file    = s3_file
end

Instance Attribute Details

#local_fileObject (readonly)

Returns the value of attribute local_file.



9
10
11
# File 'lib/s3archive/s3_file_synchronizer.rb', line 9

def local_file
  @local_file
end

#s3_fileObject (readonly)

Returns the value of attribute s3_file.



9
10
11
# File 'lib/s3archive/s3_file_synchronizer.rb', line 9

def s3_file
  @s3_file
end

Class Method Details

.run(local_path, bucket, key) ⇒ Object



30
31
32
33
34
35
# File 'lib/s3archive/s3_file_synchronizer.rb', line 30

def self.run(local_path, bucket, key)
  local_file = LocalFile.new(local_path)
  s3_file = S3File.new(bucket, key)

  new(local_file, s3_file).run
end

Instance Method Details

#runObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/s3archive/s3_file_synchronizer.rb', line 15

def run
  if s3_file.exists?
    if s3_file.md5_hex == local_file.md5_hex
      logger.info("'#{s3_file}' already exists and has correct checksum")
      nil
    else
      new_s3_file = S3File.new(s3_file.bucket, "#{s3_file.key}.#{local_file.md5_hex}")
      logger.error("'#{s3_file}' already exists and has wrong checksum. Uploading to '#{new_s3_file}'.")
      new_s3_file.put(local_file)
    end
  else
    s3_file.put(local_file)
  end
end