Class: ActiveCipherStorage::Adapters::S3Adapter

Inherits:
Object
  • Object
show all
Includes:
KeyUtils
Defined in:
lib/active_cipher_storage/adapters/s3_adapter.rb

Defined Under Namespace

Classes: StreamingDecryptor

Constant Summary collapse

DEFAULT_MULTIPART_THRESHOLD =
100 * 1024 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(bucket:, region: nil, multipart_threshold: DEFAULT_MULTIPART_THRESHOLD, s3_client: nil, config: nil) ⇒ S3Adapter

Returns a new instance of S3Adapter.



10
11
12
13
14
15
16
17
18
# File 'lib/active_cipher_storage/adapters/s3_adapter.rb', line 10

def initialize(bucket:, region: nil, multipart_threshold: DEFAULT_MULTIPART_THRESHOLD,
               s3_client: nil, config: nil)
  @bucket              = bucket
  @region              = region
  @multipart_threshold = multipart_threshold
  @client_override     = s3_client
  @config              = config || ActiveCipherStorage.configuration
  @config.validate!
end

Instance Method Details

#delete(key) ⇒ Object



55
56
57
# File 'lib/active_cipher_storage/adapters/s3_adapter.rb', line 55

def delete(key)
  s3.delete_object(bucket: @bucket, key: key)
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/active_cipher_storage/adapters/s3_adapter.rb', line 48

def exist?(key)
  s3.head_object(bucket: @bucket, key: key)
  true
rescue Aws::S3::Errors::NotFound, Aws::S3::Errors::NoSuchKey
  false
end

#get_decrypted(key) ⇒ Object



24
25
26
27
# File 'lib/active_cipher_storage/adapters/s3_adapter.rb', line 24

def get_decrypted(key)
  resp = s3.get_object(bucket: @bucket, key: key)
  decrypt_io(StringIO.new(resp.body.read.b))
end

#presigned_url(key, expires_in: 3600) ⇒ Object



42
43
44
45
46
# File 'lib/active_cipher_storage/adapters/s3_adapter.rb', line 42

def presigned_url(key, expires_in: 3600)
  Aws::S3::Presigner.new(client: s3)
                    .presigned_url(:get_object, bucket: @bucket, key: key,
                                   expires_in: expires_in)
end

#put_encrypted(key, io, **options) ⇒ Object



20
21
22
# File 'lib/active_cipher_storage/adapters/s3_adapter.rb', line 20

def put_encrypted(key, io, **options)
  large_file?(io) ? multipart_put(key, io, **options) : single_put(key, io, **options)
end

#stream_decrypted(key, &block) ⇒ Object

Streams decrypted plaintext from S3 without buffering the whole object. Yields each decrypted plaintext chunk as it becomes available. Safe for multi-gigabyte files: memory usage is bounded by chunk_size.

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
# File 'lib/active_cipher_storage/adapters/s3_adapter.rb', line 32

def stream_decrypted(key, &block)
  raise ArgumentError, "stream_decrypted requires a block" unless block_given?

  decryptor = StreamingDecryptor.new(@config)
  s3.get_object(bucket: @bucket, key: key) do |s3_chunk|
    decryptor.push(s3_chunk.b, &block)
  end
  decryptor.finish!
end