Class: RailsUploads::Storages::S3
- Inherits:
-
Object
- Object
- RailsUploads::Storages::S3
- Defined in:
- lib/rails_uploads/storages/s3.rb
Instance Method Summary collapse
- #delete(path) ⇒ Object
- #exists?(path) ⇒ Boolean
-
#initialize(default) ⇒ S3
constructor
A new instance of S3.
- #magick(source, output, upload) {|RailsUploads::Magick::Image.new(@tmp[source], tmp.path)| ... } ⇒ Object
- #size(path) ⇒ Object
- #store(upload, path) ⇒ Object
- #url(path) ⇒ Object
Constructor Details
#initialize(default) ⇒ S3
Returns a new instance of S3.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rails_uploads/storages/s3.rb', line 7 def initialize(default) if (Rails.env == 'test' and not default) env = 'test' elsif default env = 'production' else env = Rails.env end config = self.class.config[env] AWS.config access_key_id: config['access_key_id'], secret_access_key: config['secret_access_key'] @bucket = AWS::S3.new.buckets[config['bucket']] @tmp = {} end |
Instance Method Details
#delete(path) ⇒ Object
38 39 40 |
# File 'lib/rails_uploads/storages/s3.rb', line 38 def delete(path) object(path).delete end |
#exists?(path) ⇒ Boolean
21 22 23 |
# File 'lib/rails_uploads/storages/s3.rb', line 21 def exists?(path) object(path).exists? end |
#magick(source, output, upload) {|RailsUploads::Magick::Image.new(@tmp[source], tmp.path)| ... } ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rails_uploads/storages/s3.rb', line 42 def magick(source, output, upload) if @tmp[source].blank? if upload.present? @tmp[source] = upload.path else remote = Tempfile.new('s3') remote.binmode object(source).read { |chunk| remote.write(chunk) } remote.close remote.open @tmp[source] = remote.path end end tmp = Tempfile.new('s3') yield RailsUploads::Magick::Image.new(@tmp[source], tmp.path) store tmp, output end |
#size(path) ⇒ Object
25 26 27 |
# File 'lib/rails_uploads/storages/s3.rb', line 25 def size(path) object(path).content_length end |
#store(upload, path) ⇒ Object
34 35 36 |
# File 'lib/rails_uploads/storages/s3.rb', line 34 def store(upload, path) @bucket.objects.create(path, Pathname.new(upload.path)).acl = :public_read end |
#url(path) ⇒ Object
29 30 31 32 |
# File 'lib/rails_uploads/storages/s3.rb', line 29 def url(path) base_url = Rails.application.config.uploads.base_url base_url.present? ? ::File.join(base_url, path) : object(path).public_url(secure: false) end |