Class: LaserBlob::Storage::S3
- Inherits:
-
Object
- Object
- LaserBlob::Storage::S3
- Defined in:
- lib/laserblob/storage/s3.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #copy_to_tempfile(id, basename: nil, &block) ⇒ Object
- #delete(id) ⇒ Object
- #exists?(id) ⇒ Boolean
-
#initialize(config = {}) ⇒ S3
constructor
A new instance of S3.
- #local? ⇒ Boolean
- #read(id) ⇒ Object
- #url(id, disposition: 'attachment', expires_in: 300) ⇒ Object
- #write(id, file, options = {}) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ S3
Returns a new instance of S3.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/laserblob/storage/s3.rb', line 8 def initialize(config = {}) @bucket = config[:bucket] @client = Aws::S3::Client.new( access_key_id: config[:access_key_id], secret_access_key: config[:secret_access_key], region: config[:region] || 'us-east-1', endpoint: config[:endpoint] ) @resource = Aws::S3::Resource.new(client: @client) @bucket_obj = @resource.bucket(@bucket) end |
Instance Attribute Details
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
6 7 8 |
# File 'lib/laserblob/storage/s3.rb', line 6 def bucket @bucket end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
6 7 8 |
# File 'lib/laserblob/storage/s3.rb', line 6 def client @client end |
Instance Method Details
#copy_to_tempfile(id, basename: nil, &block) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/laserblob/storage/s3.rb', line 51 def copy_to_tempfile(id, basename: nil, &block) basename ||= ['blob', ''] Tempfile.create(basename, binmode: true) do |tmpfile| @bucket_obj.object(key_for(id)).get(response_target: tmpfile.path) tmpfile.rewind block.call(tmpfile) end end |
#delete(id) ⇒ Object
35 36 37 |
# File 'lib/laserblob/storage/s3.rb', line 35 def delete(id) @bucket_obj.object(key_for(id)).delete end |
#exists?(id) ⇒ Boolean
39 40 41 |
# File 'lib/laserblob/storage/s3.rb', line 39 def exists?(id) @bucket_obj.object(key_for(id)).exists? end |
#local? ⇒ Boolean
20 21 22 |
# File 'lib/laserblob/storage/s3.rb', line 20 def local? false end |
#read(id) ⇒ Object
31 32 33 |
# File 'lib/laserblob/storage/s3.rb', line 31 def read(id) @bucket_obj.object(key_for(id)).get.body.read end |
#url(id, disposition: 'attachment', expires_in: 300) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/laserblob/storage/s3.rb', line 43 def url(id, disposition: 'attachment', expires_in: 300) @bucket_obj.object(key_for(id)).presigned_url( :get, expires_in: expires_in, response_content_disposition: disposition ) end |
#write(id, file, options = {}) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/laserblob/storage/s3.rb', line 24 def write(id, file, = {}) @bucket_obj.object(key_for(id)).upload_file( file.path, content_type: [:content_type] ) end |