Class: Refile::Backend::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/refile/backend/s3.rb

Overview

A refile backend which stores files in Amazon S3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key_id:, secret_access_key:, bucket:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, **s3_options) ⇒ S3

Returns a new instance of S3.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/refile/backend/s3.rb', line 10

def initialize(access_key_id:, secret_access_key:, bucket:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, **s3_options)
  @access_key_id = access_key_id
  @secret_access_key = secret_access_key
  @s3_options = { access_key_id: access_key_id, secret_access_key: secret_access_key }.merge s3_options
  @s3 = AWS::S3.new @s3_options
  @bucket_name = bucket
  @bucket = @s3.buckets[@bucket_name]
  @hasher = hasher
  @prefix = prefix
  @max_size = max_size
end

Instance Attribute Details

#access_key_idObject (readonly)

Returns the value of attribute access_key_id.



8
9
10
# File 'lib/refile/backend/s3.rb', line 8

def access_key_id
  @access_key_id
end

#max_sizeObject (readonly)

Returns the value of attribute max_size.



8
9
10
# File 'lib/refile/backend/s3.rb', line 8

def max_size
  @max_size
end

Instance Method Details

#clear!(confirm = nil) ⇒ Object

Raises:



64
65
66
67
# File 'lib/refile/backend/s3.rb', line 64

def clear!(confirm = nil)
  raise Refile::Confirm unless confirm == :confirm
  @bucket.objects.with_prefix(@prefix).delete_all
end

#delete(id) ⇒ Object



40
41
42
# File 'lib/refile/backend/s3.rb', line 40

def delete(id)
  object(id).delete
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/refile/backend/s3.rb', line 60

def exists?(id)
  object(id).exists?
end

#get(id) ⇒ Object



36
37
38
# File 'lib/refile/backend/s3.rb', line 36

def get(id)
  Refile::File.new(self, id)
end

#object(id) ⇒ Object



76
77
78
# File 'lib/refile/backend/s3.rb', line 76

def object(id)
  @bucket.objects[[*@prefix, id].join("/")]
end

#open(id) ⇒ Object



44
45
46
# File 'lib/refile/backend/s3.rb', line 44

def open(id)
  Kernel.open(object(id).url_for(:read))
end

#presignObject



69
70
71
72
73
74
# File 'lib/refile/backend/s3.rb', line 69

def presign
  id = RandomHasher.new.hash
  signature = @bucket.presigned_post(key: [*@prefix, id].join("/"))
  signature.where(content_length: @max_size) if @max_size
  Signature.new(as: "file", id: id, url: signature.url.to_s, fields: signature.fields)
end

#read(id) ⇒ Object



48
49
50
51
52
# File 'lib/refile/backend/s3.rb', line 48

def read(id)
  object(id).read
rescue AWS::S3::Errors::NoSuchKey
  nil
end

#size(id) ⇒ Object



54
55
56
57
58
# File 'lib/refile/backend/s3.rb', line 54

def size(id)
  object(id).content_length
rescue AWS::S3::Errors::NoSuchKey
  nil
end

#upload(uploadable) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/refile/backend/s3.rb', line 22

def upload(uploadable)
  Refile.verify_uploadable(uploadable, @max_size)

  id = @hasher.hash(uploadable)

  if uploadable.is_a?(Refile::File) and uploadable.backend.is_a?(S3) and uploadable.backend.access_key_id == access_key_id
    uploadable.backend.object(uploadable.id).copy_to(object(id))
  else
    object(id).write(uploadable, content_length: uploadable.size)
  end

  Refile::File.new(self, id)
end