Class: S3Sync::Download

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_sync/download.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = S3Sync.configuration) ⇒ Download

Returns a new instance of Download.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/s3_sync/download.rb', line 5

def initialize(options = S3Sync.configuration)
  @key_id = options.key_id
  @key_secret = options.key_secret
  @region = options.region
  @bucket = options.bucket
  @secret_phrase = options.secret_phrase
  @files = options.files
  @downloads_dir = options.downloads_dir
  raise "check your configuration" unless [@key_id, @key_secret, @region, @bucket, @secret_phrase, @downloads_dir].map{|s| s.class}.uniq == [String] && @files.is_a?(Array)

  creds = Aws::Credentials.new(@key_id, @key_secret)
  client = Aws::S3::Client.new(:region => @region, :credentials => creds)

  # Find latest uploads directory.

  object_keys = []
  objects = client.list_objects(:bucket => @bucket)

  if objects.next_page?
    while objects.last_page? == false
      object_keys << objects.contents.map{|obj| obj.key}
      objects = objects.next_page
    end
  else
    object_keys << objects.contents.map{|obj| obj.key}
  end

  object_directories = object_keys.flatten.map{|str| str.split("/").first}.compact.uniq
  days = object_directories.map{|str| Date.parse(str) rescue nil}.compact
  latest_day = days.max.to_s

  # Create downloads directory.

  FileUtils.mkdir_p(@downloads_dir)

  # Download files.

  files.each do |file|
    s3_file = File.join(latest_day, file)
    local_file = File.join(@downloads_dir, @bucket, file)
    FileUtils.mkdir_p(local_file.gsub(local_file.split("/").last,""))
    client.get_object({:bucket => @bucket, :key => s3_file}, target: local_file)
    decrypted_file_contents = Encryptor.decrypt(File.read(local_file), :key => secret_phrase)
    File.write(local_file, decrypted_file_contents)
    puts "downloaded and decrypted #{@bucket}/#{s3_file} to #{local_file}"
  end
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



3
4
5
# File 'lib/s3_sync/download.rb', line 3

def bucket
  @bucket
end

#downloads_dirObject

Returns the value of attribute downloads_dir.



3
4
5
# File 'lib/s3_sync/download.rb', line 3

def downloads_dir
  @downloads_dir
end

#filesObject

Returns the value of attribute files.



3
4
5
# File 'lib/s3_sync/download.rb', line 3

def files
  @files
end

#key_idObject

Returns the value of attribute key_id.



3
4
5
# File 'lib/s3_sync/download.rb', line 3

def key_id
  @key_id
end

#key_secretObject

Returns the value of attribute key_secret.



3
4
5
# File 'lib/s3_sync/download.rb', line 3

def key_secret
  @key_secret
end

#regionObject

Returns the value of attribute region.



3
4
5
# File 'lib/s3_sync/download.rb', line 3

def region
  @region
end

#secret_phraseObject

Returns the value of attribute secret_phrase.



3
4
5
# File 'lib/s3_sync/download.rb', line 3

def secret_phrase
  @secret_phrase
end