Module: Paperclip::Storage::Aws
- Defined in:
- lib/paperclip-aws.rb
Class Method Summary collapse
Instance Method Summary collapse
- #bucket ⇒ Object
- #choose_protocol(style, options = {}) ⇒ Object
- #copy_to_local_file(style, local_dest_path) ⇒ Object
- #create_bucket ⇒ Object
- #exists?(style = default_style) ⇒ Boolean
-
#flush_deletes ⇒ Object
:nodoc:.
-
#flush_writes ⇒ Object
:nodoc:.
- #s3 ⇒ Object
- #s3_bucket=(value) ⇒ Object
-
#to_file(style = default_style) ⇒ Object
Returns representation of the data of the file assigned to the given style, in the format most representative of the current storage.
- #url(style = default_style, options = {}) ⇒ Object
- #versions(style = default_style) ⇒ Object
Class Method Details
.extended(base) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/paperclip-aws.rb', line 10 def self.extended base begin require 'aws-sdk' rescue LoadError => e e. << " (You may need to install the aws-sdk gem)" raise e end unless defined?(AWS::Core) attr_accessor :s3_credentials, :s3_bucket, :s3_permissions, :s3_options, :s3_protocol, :s3_host_alias base.instance_eval do self.setup_s3_credentials self.setup_s3_bucket self. self.setup_s3_protocol self. self.setup_s3_host_alias end end |
Instance Method Details
#bucket ⇒ Object
62 63 64 |
# File 'lib/paperclip-aws.rb', line 62 def bucket @bucket ||= self.s3.buckets[@s3_bucket] end |
#choose_protocol(style, options = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/paperclip-aws.rb', line 94 def choose_protocol(style, ={}) protocol = if [:protocol].present? [:protocol].to_s else @s3_protocol.is_a?(Proc) ? @s3_protocol.call(style, self) : @s3_protocol end "#{protocol}:" if protocol.present? end |
#copy_to_local_file(style, local_dest_path) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/paperclip-aws.rb', line 154 def copy_to_local_file(style, local_dest_path) log("copying #{path(style)} to local file #{local_dest_path}") local_file = ::File.open(local_dest_path, 'wb') file = self.s3.buckets[@s3_bucket].objects[self.path(style)] local_file.write(file.read) local_file.close rescue AWS::Errors::Base => e warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}") false end |
#create_bucket ⇒ Object
118 119 120 |
# File 'lib/paperclip-aws.rb', line 118 def create_bucket self.s3.buckets.create(@s3_bucket) end |
#exists?(style = default_style) ⇒ Boolean
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/paperclip-aws.rb', line 83 def exists?(style = default_style) if path(style).nil? || path(style).to_s.strip == "" return false end begin return self.bucket.objects[path(style)].exists? rescue AWS::S3::Errors::Base return false end end |
#flush_deletes ⇒ Object
:nodoc:
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/paperclip-aws.rb', line 142 def flush_deletes #:nodoc: @queued_for_delete.each do |path| begin log "Deleting #{path}" self.s3.buckets[@s3_bucket].objects[path].delete rescue AWS::Errors::Base => e raise end end @queued_for_delete = [] end |
#flush_writes ⇒ Object
:nodoc:
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/paperclip-aws.rb', line 122 def flush_writes #:nodoc: @queued_for_write.each do |style, file| begin log "Saving #{path(style)}" self.s3.buckets[@s3_bucket].objects[path(style)].write({ :file => file.path, :acl => @s3_permissions[:style.to_sym] || @s3_permissions[:default], :content_type => file.content_type.to_s.strip }.reverse_merge(@s3_options)) rescue AWS::S3::Errors::NoSuchBucket => e create_bucket retry rescue AWS::Errors::Base => e raise end end @queued_for_write = {} end |
#s3 ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/paperclip-aws.rb', line 30 def s3 @s3 ||= AWS::S3.new( :access_key_id => @s3_credentials[:access_key_id], :secret_access_key => @s3_credentials[:secret_access_key], :s3_endpoint => @s3_endpoint ) end |
#s3_bucket=(value) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/paperclip-aws.rb', line 66 def s3_bucket=(value) @bucket = nil @s3_bucket = value @s3_bucket = @s3_bucket.call(self) if @s3_bucket.is_a?(Proc) end |
#to_file(style = default_style) ⇒ Object
Returns representation of the data of the file assigned to the given style, in the format most representative of the current storage.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/paperclip-aws.rb', line 106 def to_file(style = default_style) return @queued_for_write[style] if @queued_for_write[style] filename = path(style) extname = File.extname(filename) basename = File.basename(filename, extname) file = Tempfile.new([basename, extname]) file.binmode file.write(self.bucket.objects[path(style)].read) file.rewind return file end |
#url(style = default_style, options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/paperclip-aws.rb', line 38 def url(style=default_style, ={}) if self.original_filename.nil? # default_url = @default_url.is_a?(Proc) ? @default_url.call(self) : @default_url # return interpolate(default_url, style) return super end if [:expires].present? || [:action].present? .reverse_merge!({ :expires => 60*60, :action => :read }) secure = ( self.choose_protocol(style, ) == 'https:' ) return self.s3.buckets[@s3_bucket].objects[path(style).gsub(%r{^/}, "")].url_for([:action], { :secure => secure, :expires => [:expires] }).to_s else if @s3_host_alias.present? url = "#{choose_protocol(style, )}//#{@s3_host_alias}/#{path(style).gsub(%r{^/}, "")}" else url = "#{choose_protocol(style, )}//#{@s3_endpoint}/#{@s3_bucket}/#{path(style).gsub(%r{^/}, "")}" end return URI.escape(url) end end |
#versions(style = default_style) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/paperclip-aws.rb', line 73 def versions(style = default_style) begin return nil unless self.bucket.versioning_enabled? self.bucket.objects[path(style)].versions rescue AWS::S3::Errors::Base => e log "ERROR: Error occured while fetching versions for `#{path(style)}`. Probably versions are disabled or suspended for `#{self.bucket.name}` bucket." return nil end end |