Class: Usmu::S3::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/usmu/s3/uploader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, s3_configuration) ⇒ Uploader

Returns a new instance of Uploader.

Parameters:



7
8
9
10
11
12
# File 'lib/usmu/s3/uploader.rb', line 7

def initialize(configuration, s3_configuration)
  @log = Logging.logger[self]
  @configuration = configuration
  s3 = Aws::S3::Resource.new(credentials: s3_configuration.credentials, region: s3_configuration.region)
  @bucket = s3.bucket(s3_configuration.bucket)
end

Instance Attribute Details

#bucketvoid (readonly, private)

Returns the value of attribute bucket.



24
25
26
# File 'lib/usmu/s3/uploader.rb', line 24

def bucket
  @bucket
end

#configurationvoid (readonly, private)

Returns the value of attribute configuration.



23
24
25
# File 'lib/usmu/s3/uploader.rb', line 23

def configuration
  @configuration
end

#logvoid (readonly, private)

Returns the value of attribute log.



22
23
24
# File 'lib/usmu/s3/uploader.rb', line 22

def log
  @log
end

Instance Method Details

#delete_remote(files) ⇒ void (private)



38
39
40
41
42
43
44
45
46
47
# File 'lib/usmu/s3/uploader.rb', line 38

def delete_remote(files)
  # We can 'only' delete 1000 items at a time with #delete_objects.
  for i in (0...files.length).step(1000)
    deleting = files.slice(i, 1000)
    for f in deleting
      @log.success("Deleting #{f}")
    end
    @bucket.delete_objects({delete: {objects: deleting.map { |f| {key: f} }}})
  end
end

#push(diff) ⇒ void



14
15
16
17
18
# File 'lib/usmu/s3/uploader.rb', line 14

def push(diff)
  push_local(diff[:local])
  push_local(diff[:updated])
  delete_remote(diff[:remote])
end

#push_local(files) ⇒ void (private)



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/usmu/s3/uploader.rb', line 26

def push_local(files)
  for file in files
    @log.success("Uploading #{file}")
    File.open(File.join(@configuration.destination_path, file), 'r') do |io|
      @bucket.put_object({
                             key: file,
                             body: io,
                         })
    end
  end
end