Class: S3Sync::Upload

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = S3Sync.configuration) ⇒ Upload

Returns a new instance of Upload.



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
# File 'lib/s3_sync/upload.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
  raise "check your configuration" unless [@key_id, @key_secret, @region, @bucket, @secret_phrase].map{|s| s.class}.uniq == [String] && @files.is_a?(Array)

  creds = Aws::Credentials.new(@key_id, @key_secret)

  # Create bucket.

  resource = Aws::S3::Resource.new(:region => @region, credentials: creds)
  bucket_names = resource.buckets.map{|b| b.name}
  resource.create_bucket(:bucket => @bucket, :acl => "private") unless bucket_names.include?(@bucket)

  # Upload files

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

  @files.each do |file|
    raise "couldn't find file #{file}" unless File.exist?(file)
    encrypted_file_contents = Encryptor.encrypt(File.read(file), :key => @secret_phrase)
    s3_file = File.join(Date.today.to_s, file)
    client.put_object(:bucket => @bucket, :key => s3_file, :body => encrypted_file_contents)
    puts "uploaded encrypted contents of #{file} to #{@bucket}/#{s3_file}"
  end
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



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

def bucket
  @bucket
end

#filesObject

Returns the value of attribute files.



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

def files
  @files
end

#key_idObject

Returns the value of attribute key_id.



3
4
5
# File 'lib/s3_sync/upload.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/upload.rb', line 3

def key_secret
  @key_secret
end

#regionObject

Returns the value of attribute region.



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

def region
  @region
end

#secret_phraseObject

Returns the value of attribute secret_phrase.



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

def secret_phrase
  @secret_phrase
end