Class: Selectel::S3::Uploader

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

Instance Method Summary collapse

Constructor Details

#initialize(container_name) ⇒ Uploader

Returns a new instance of Uploader.



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

def initialize(container_name)
  @s3_resource = Aws::S3::Resource.new(
    region: config.region, access_key_id: config.access_key_id,
    secret_access_key: config.secret_access_key, endpoint: config.endpoint
  )
  @container_name = container_name
end

Instance Method Details

#upload_file(file_path, remote_path = file_path) ⇒ Object



25
26
27
28
29
# File 'lib/selectel/s3/uploader.rb', line 25

def upload_file(file_path, remote_path = file_path)
  object = s3_resource.bucket(container_name).object(remote_path)

  object.upload_file(file_path)
end

#upload_folder(folder_path, remote_path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/selectel/s3/uploader.rb', line 31

def upload_folder(folder_path, remote_path)
  started_at = Time.now
  absolute_folder_path = File.expand_path(folder_path)
  files = fetch_all_files(absolute_folder_path)

  files.each do |file_path|
    remote_file_path = file_path.sub(absolute_folder_path, remote_path)

    upload_file(file_path, remote_file_path)
  end

  puts "Uploaded #{ files.size } files for #{ Time.now - started_at } seconds"
end

#upload_remote_file(file_url, remote_path) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/selectel/s3/uploader.rb', line 15

def upload_remote_file(file_url, remote_path)
  object = s3_resource.bucket(container_name).object(remote_path)

  file = fetch_file(file_url)

  object.upload_file(file)

  file.delete
end