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.



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

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



43
44
45
46
47
48
49
50
51
52
# File 'lib/crossing.rb', line 43

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



62
63
64
65
66
67
68
69
# File 'lib/crossing.rb', line 62

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



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

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

#put(bucket, filename) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/crossing.rb', line 28

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



36
37
38
39
40
41
# File 'lib/crossing.rb', line 36

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



58
59
60
# File 'lib/crossing.rb', line 58

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
14
# 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