Class: Defile::Backend::S3

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

Defined Under Namespace

Classes: Reader, Signature

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of S3.



59
60
61
62
63
64
65
66
67
68
# File 'lib/defile/backend/s3.rb', line 59

def initialize(access_key_id:, secret_access_key:, bucket:, max_size: nil, prefix: nil, hasher: Defile::RandomHasher.new)
  @access_key_id = access_key_id
  @secret_access_key = secret_access_key
  @s3 = AWS::S3.new(access_key_id: access_key_id, secret_access_key: secret_access_key)
  @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.



57
58
59
# File 'lib/defile/backend/s3.rb', line 57

def access_key_id
  @access_key_id
end

Instance Method Details

#clear!(confirm = nil) ⇒ Object

Raises:

  • (ArgumentError)


112
113
114
115
# File 'lib/defile/backend/s3.rb', line 112

def clear!(confirm = nil)
  raise ArgumentError, "are you sure? this will remove all files in the backend, call as `clear!(:confirm)` if you're sure you want to do this" unless confirm == :confirm
  @bucket.objects.with_prefix(@prefix).delete_all
end

#delete(id) ⇒ Object



88
89
90
# File 'lib/defile/backend/s3.rb', line 88

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

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/defile/backend/s3.rb', line 108

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

#get(id) ⇒ Object



84
85
86
# File 'lib/defile/backend/s3.rb', line 84

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

#object(id) ⇒ Object



124
125
126
# File 'lib/defile/backend/s3.rb', line 124

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

#open(id) ⇒ Object



92
93
94
# File 'lib/defile/backend/s3.rb', line 92

def open(id)
  Reader.new(object(id))
end

#presignObject



117
118
119
120
121
122
# File 'lib/defile/backend/s3.rb', line 117

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("file", id, signature.url.to_s, signature.fields)
end

#read(id) ⇒ Object



96
97
98
99
100
# File 'lib/defile/backend/s3.rb', line 96

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

#size(id) ⇒ Object



102
103
104
105
106
# File 'lib/defile/backend/s3.rb', line 102

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

#upload(uploadable) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/defile/backend/s3.rb', line 70

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

  id = @hasher.hash(uploadable)

  if uploadable.is_a?(Defile::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

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