Class: Whoosh::Storage::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/storage/s3.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket:, region: "us-east-1", access_key_id: nil, secret_access_key: nil) ⇒ S3

Returns a new instance of S3.



18
19
20
21
22
# File 'lib/whoosh/storage/s3.rb', line 18

def initialize(bucket:, region: "us-east-1", access_key_id: nil, secret_access_key: nil)
  raise Errors::DependencyError, "S3 storage requires the 'aws-sdk-s3' gem" unless self.class.available?
  @bucket = bucket
  @client = Aws::S3::Client.new(region: region, access_key_id: access_key_id, secret_access_key: secret_access_key)
end

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
# File 'lib/whoosh/storage/s3.rb', line 11

def self.available?
  if @aws_available.nil?
    @aws_available = begin; require "aws-sdk-s3"; true; rescue LoadError; false; end
  end
  @aws_available
end

Instance Method Details

#save(uploaded_file, prefix = "") ⇒ Object



24
25
26
27
28
# File 'lib/whoosh/storage/s3.rb', line 24

def save(uploaded_file, prefix = "")
  key = prefix.empty? ? "#{SecureRandom.uuid}_#{uploaded_file.filename}" : "#{prefix}/#{SecureRandom.uuid}_#{uploaded_file.filename}"
  @client.put_object(bucket: @bucket, key: key, body: uploaded_file.read)
  key
end