Class: Refile::S3
- Inherits:
-
Object
- Object
- Refile::S3
- Extended by:
- BackendMacros
- Defined in:
- lib/refile/s3.rb,
lib/refile/s3/version.rb
Overview
A refile backend which stores files in Amazon S3
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
readonly
Returns the value of attribute access_key_id.
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
Instance Method Summary collapse
-
#clear!(confirm = nil) ⇒ void
Remove all files in this backend.
-
#delete ⇒ void
Delete a file from this backend.
-
#exists? ⇒ Boolean
Return whether the file with the given id exists in this backend.
-
#get ⇒ Refile::File
Get a file from this backend.
-
#initialize(access_key_id:, secret_access_key:, region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, **s3_options) ⇒ S3
constructor
Sets up an S3 backend with the given credentials.
-
#open ⇒ IO
Return an IO object for the uploaded file which can be used to read its content.
-
#presign ⇒ Refile::Signature
Return a presign signature which can be used to upload a file into this backend directly.
-
#read ⇒ String
Return the entire contents of the uploaded file as a String.
-
#size ⇒ Integer
Return the size in bytes of the uploaded file.
-
#upload ⇒ Refile::File
Upload a file into this backend.
Constructor Details
#initialize(access_key_id:, secret_access_key:, region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, **s3_options) ⇒ S3
Sets up an S3 backend with the given credentials.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/refile/s3.rb', line 36 def initialize(access_key_id:, secret_access_key:, region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, **) @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, region: region }.merge @s3 = Aws::S3::Resource.new @s3_options @bucket_name = bucket @bucket = @s3.bucket @bucket_name @hasher = hasher @prefix = prefix @max_size = max_size end |
Instance Attribute Details
#access_key_id ⇒ Object (readonly)
Returns the value of attribute access_key_id.
22 23 24 |
# File 'lib/refile/s3.rb', line 22 def access_key_id @access_key_id end |
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
22 23 24 |
# File 'lib/refile/s3.rb', line 22 def max_size @max_size end |
Instance Method Details
#clear!(confirm = nil) ⇒ void
This method returns an undefined value.
Remove all files in this backend. You must confirm the deletion by passing the symbol ‘:confirm` as an argument to this method.
129 130 131 132 |
# File 'lib/refile/s3.rb', line 129 def clear!(confirm = nil) raise Refile::Confirm unless confirm == :confirm @bucket.objects(prefix: @prefix).delete end |
#delete ⇒ void
This method returns an undefined value.
Delete a file from this backend
80 81 82 |
# File 'lib/refile/s3.rb', line 80 verify_id def delete(id) object(id).delete end |
#exists? ⇒ Boolean
Return whether the file with the given id exists in this backend.
117 118 119 |
# File 'lib/refile/s3.rb', line 117 verify_id def exists?(id) object(id).exists? end |
#get ⇒ Refile::File
Get a file from this backend.
Note that this method will always return a File object, even if a file with the given id does not exist in this backend. Use FileSystem#exists? to check if the file actually exists.
72 73 74 |
# File 'lib/refile/s3.rb', line 72 verify_id def get(id) Refile::File.new(self, id) end |
#open ⇒ IO
Return an IO object for the uploaded file which can be used to read its content.
89 90 91 |
# File 'lib/refile/s3.rb', line 89 verify_id def open(id) Kernel.open(object(id).presigned_url(:get)) end |
#presign ⇒ Refile::Signature
Return a presign signature which can be used to upload a file into this backend directly.
138 139 140 141 142 143 |
# File 'lib/refile/s3.rb', line 138 def presign id = RandomHasher.new.hash signature = @bucket.presigned_post(key: [*@prefix, id].join("/")) signature.content_length_range(0..@max_size) if @max_size Signature.new(as: "file", id: id, url: signature.url.to_s, fields: signature.fields) end |
#read ⇒ String
Return the entire contents of the uploaded file as a String.
97 98 99 100 101 |
# File 'lib/refile/s3.rb', line 97 verify_id def read(id) object(id).get.body.read rescue Aws::S3::Errors::NoSuchKey nil end |
#size ⇒ Integer
Return the size in bytes of the uploaded file.
107 108 109 110 111 |
# File 'lib/refile/s3.rb', line 107 verify_id def size(id) object(id).get.content_length rescue Aws::S3::Errors::NoSuchKey nil end |
#upload ⇒ Refile::File
Upload a file into this backend
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/refile/s3.rb', line 52 verify_uploadable def upload(uploadable) 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 object(id).copy_from(copy_source: [@bucket_name, uploadable.backend.object(uploadable.id).key].join("/")) else object(id).put(body: uploadable, content_length: uploadable.size) end Refile::File.new(self, id) end |