Class: S3Sync::Download
- Inherits:
-
Object
- Object
- S3Sync::Download
- Defined in:
- lib/s3_sync/download.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#downloads_dir ⇒ Object
Returns the value of attribute downloads_dir.
-
#files ⇒ Object
Returns the value of attribute files.
-
#key_id ⇒ Object
Returns the value of attribute key_id.
-
#key_secret ⇒ Object
Returns the value of attribute key_secret.
-
#region ⇒ Object
Returns the value of attribute region.
-
#secret_phrase ⇒ Object
Returns the value of attribute secret_phrase.
Instance Method Summary collapse
-
#initialize(options = S3Sync.configuration) ⇒ Download
constructor
A new instance of Download.
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( = S3Sync.configuration) @key_id = .key_id @key_secret = .key_secret @region = .region @bucket = .bucket @secret_phrase = .secret_phrase @files = .files @downloads_dir = .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
#bucket ⇒ Object
Returns the value of attribute bucket.
3 4 5 |
# File 'lib/s3_sync/download.rb', line 3 def bucket @bucket end |
#downloads_dir ⇒ Object
Returns the value of attribute downloads_dir.
3 4 5 |
# File 'lib/s3_sync/download.rb', line 3 def downloads_dir @downloads_dir end |
#files ⇒ Object
Returns the value of attribute files.
3 4 5 |
# File 'lib/s3_sync/download.rb', line 3 def files @files end |
#key_id ⇒ Object
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_secret ⇒ Object
Returns the value of attribute key_secret.
3 4 5 |
# File 'lib/s3_sync/download.rb', line 3 def key_secret @key_secret end |
#region ⇒ Object
Returns the value of attribute region.
3 4 5 |
# File 'lib/s3_sync/download.rb', line 3 def region @region end |
#secret_phrase ⇒ Object
Returns the value of attribute secret_phrase.
3 4 5 |
# File 'lib/s3_sync/download.rb', line 3 def secret_phrase @secret_phrase end |