Class: Crossing

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

Overview

Documentation incoming

Instance Method Summary collapse

Constructor Details

#initialize(s3_client_encrypted, s3_client_unencrypted = nil) ⇒ Crossing

Returns a new instance of Crossing.



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

def initialize(s3_client_encrypted, s3_client_unencrypted = nil)
  raise CrossingMisconfigurationException if s3_client_encrypted.nil?
  unless s3_client_encrypted.is_a? Aws::S3::Encryption::Client
    raise CrossingMisconfigurationException
  end
  @s3_client_encrypted = s3_client_encrypted

  setup_unencrypted_client s3_client_encrypted, s3_client_unencrypted
end

Instance Method Details

#get(bucket, file) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/crossing.rb', line 40

def get(bucket, file)
  if File.exist?(file)
    raise(CrossingFileExistsException,
          "File #{file} already exists, will not overwrite.")
  end

  content = get_content(bucket, file)

  File.open(file, 'wb') { |f| f.write(content) }
end

#get_content(bucket, file) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/crossing.rb', line 59

def get_content(bucket, file)
  @s3_client_encrypted.get_object(bucket: bucket, key: file).body.read

# If a decryption exception occurs, warn and get the object without decryption
rescue Aws::S3::Encryption::Errors::DecryptionError
  STDERR.puts "WARNING: #{file} decryption failed. Retreiving the object without encryption."
  @s3_client_unencrypted.get_object(bucket: bucket, key: file).body.read
end

#get_multiple(bucket, filelist) ⇒ Object



51
52
53
# File 'lib/crossing.rb', line 51

def get_multiple(bucket, filelist)
  filelist.each { |file| get(bucket, file) }
end

#put(bucket, filename) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/crossing.rb', line 25

def put(bucket, filename)
  File.open(filename, 'r') do |file|
    put_content(bucket, File.basename(filename), file.read)
  end
rescue Errno::ENOENT
  raise CrossingFileNotFoundException, "File not found: #{filename}"
end

#put_content(bucket, filename, content) ⇒ Object



33
34
35
36
37
38
# File 'lib/crossing.rb', line 33

def put_content(bucket, filename, content)
  @s3_client_encrypted.put_object(bucket: bucket,
                                  key: filename,
                                  body: content,
                                  metadata: { 'x-crossing-uploaded' => 'true' })
end

#put_multiple(bucket, filelist) ⇒ Object



55
56
57
# File 'lib/crossing.rb', line 55

def put_multiple(bucket, filelist)
  filelist.each { |file| put(bucket, file) }
end

#setup_unencrypted_client(s3_client_encrypted, s3_client_unencrypted) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/crossing.rb', line 5

def setup_unencrypted_client(s3_client_encrypted, s3_client_unencrypted)
  if !s3_client_unencrypted.nil?
    raise CrossingMisconfigurationException unless s3_client_unencrypted.is_a? Aws::S3::Client
    @s3_client_unencrypted = s3_client_unencrypted
  else
    # assign regular s3 client
    @s3_client_unencrypted = s3_client_encrypted.client
  end
end