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.



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

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

  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

Instance Method Details

#get(bucket, file) ⇒ Object



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

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



53
54
55
56
57
58
59
60
# File 'lib/crossing.rb', line 53

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



45
46
47
# File 'lib/crossing.rb', line 45

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

#put(bucket, filename) ⇒ Object



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

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



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

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

#put_multiple(bucket, filelist) ⇒ Object



49
50
51
# File 'lib/crossing.rb', line 49

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