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.2.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(region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, **s3_options) ⇒ S3
constructor
Sets up an S3 backend.
-
#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(region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, **s3_options) ⇒ S3
Sets up an S3 backend
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/refile/s3.rb', line 43 def initialize(region:, bucket:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, **) @s3_options = { region: region }.merge @s3 = Aws::S3::Resource.new @s3_options credentials = @s3.client.config.credentials raise S3CredentialsError unless credentials @access_key_id = credentials.access_key_id @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.
31 32 33 |
# File 'lib/refile/s3.rb', line 31 def access_key_id @access_key_id end |
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
31 32 33 |
# File 'lib/refile/s3.rb', line 31 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.
137 138 139 140 |
# File 'lib/refile/s3.rb', line 137 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
88 89 90 |
# File 'lib/refile/s3.rb', line 88 verify_id def delete(id) object(id).delete end |
#exists? ⇒ Boolean
Return whether the file with the given id exists in this backend.
125 126 127 |
# File 'lib/refile/s3.rb', line 125 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.
80 81 82 |
# File 'lib/refile/s3.rb', line 80 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.
97 98 99 |
# File 'lib/refile/s3.rb', line 97 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.
146 147 148 149 150 151 |
# File 'lib/refile/s3.rb', line 146 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.
105 106 107 108 109 |
# File 'lib/refile/s3.rb', line 105 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.
115 116 117 118 119 |
# File 'lib/refile/s3.rb', line 115 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
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/refile/s3.rb', line 60 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 |