Module: Paperclip::Storage::AwsCore
- Defined in:
- lib/paperclip/aws/core.rb
Class Method Summary collapse
Instance Method Summary collapse
- #bucket_name ⇒ Object
- #copy_to_local_file(style, local_dest_path) ⇒ Object
- #exists?(style = default_style) ⇒ Boolean
- #expiring_url(time = 3600, style_name = default_style) ⇒ Object
-
#flush_deletes ⇒ Object
:nodoc:.
-
#flush_writes ⇒ Object
:nodoc:.
- #obtain_s3_instance_for(options) ⇒ Object
- #s3_credentials ⇒ Object
- #s3_endpoint ⇒ Object
- #s3_host_alias ⇒ Object
- #s3_host_name ⇒ Object
- #s3_interface ⇒ Object
- #s3_protocol(style = default_style, with_colon = false) ⇒ Object
- #s3_url_options ⇒ Object
Class Method Details
.extended(base) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/paperclip/aws/core.rb', line 6 def self.extended base begin require 'aws-core-sdk' rescue LoadError => e e. << " (You may need to install the aws-core-sdk gem)" raise e end unless defined?(Aws) base.instance_eval do @s3_options = @options[:s3_options] || {} @s3_endpoint = @options[:s3_endpoint] @s3_protocol = @options[:s3_protocol] || Proc.new do |style, | = (@s3_permissions[style.to_s.to_sym] || @s3_permissions[:default]) = .call(, style) if .respond_to?(:call) ( == :public_read) ? 'http' : 'https' end unless @options[:url].to_s.match(/\A:s3.*url\Z/) || @options[:url] == ":asset_host" @options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/\A:rails_root\/public\/system/, '') @options[:url] = ":s3_path_url" end @options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol) @http_proxy = @options[:http_proxy] || nil end Paperclip.interpolates(:s3_alias_url) do |, style| "#{.s3_protocol(style, true)}//#{.s3_host_alias}/#{.path(style).gsub(%r{\A/}, "")}" end unless Paperclip::Interpolations.respond_to? :s3_alias_url Paperclip.interpolates(:s3_path_url) do |, style| "#{.s3_protocol(style, true)}//#{.s3_host_alias}/#{.path(style).gsub(%r{\A/}, "")}" end unless Paperclip::Interpolations.respond_to? :s3_path_url Paperclip.interpolates(:s3_domain_url) do |, style| "#{.s3_protocol(style, true)}//#{.s3_host_alias}/#{.path(style).gsub(%r{\A/}, "")}" end unless Paperclip::Interpolations.respond_to? :s3_domain_url Paperclip.interpolates(:asset_host) do |, style| "#{.path(style).gsub(%r{\A/}, "")}" end unless Paperclip::Interpolations.respond_to? :asset_host end |
Instance Method Details
#bucket_name ⇒ Object
82 83 84 85 86 |
# File 'lib/paperclip/aws/core.rb', line 82 def bucket_name @bucket = @options[:bucket] || s3_credentials[:bucket] @bucket = @bucket.call(self) if @bucket.respond_to?(:call) @bucket or raise ArgumentError, "missing required :bucket option" end |
#copy_to_local_file(style, local_dest_path) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/paperclip/aws/core.rb', line 171 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 = s3_interface.get_object(bucket: bucket_name, key: path(style)) file.body.pos = 0 local_file.write(file.body.read) local_file.close rescue AWS::Errors::Base => e warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}") false end |
#exists?(style = default_style) ⇒ Boolean
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/paperclip/aws/core.rb', line 106 def exists?(style = default_style) if original_filename s3_interface.get_object(bucket: bucket_name, key: style) true else false end rescue => e false end |
#expiring_url(time = 3600, style_name = default_style) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/paperclip/aws/core.rb', line 51 def expiring_url(time = 3600, style_name = default_style) if path(style_name) = { :expires => time, :secure => use_secure_protocol?(style_name) } s3_object(style_name).url_for(:read, .merge()).to_s else url(style_name) end end |
#flush_deletes ⇒ Object
:nodoc:
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/paperclip/aws/core.rb', line 158 def flush_deletes #:nodoc: @queued_for_delete.each do |path| begin log("deleting #{path}") s3_interface.delete_object(bucket: bucket_name, key: path.sub(%r{\A/},'')) rescue Aws::Errors::Base => e # Ignore this. end end @queued_for_delete = [] end |
#flush_writes ⇒ Object
:nodoc:
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/paperclip/aws/core.rb', line 129 def flush_writes #:nodoc: @queued_for_write.each do |style, file| begin log("saving #{path(style)}") = { bucket: bucket_name, key: path(style), acl: 'public-read', body: File.read(file.path), content_type: file.content_type } s3_interface.put_object() rescue => e log("Error: #{e.inspect}") # create_bucket # retry ensure file.rewind end end after_flush_writes # allows attachment to clean up temp files @queued_for_write = {} end |
#obtain_s3_instance_for(options) ⇒ Object
96 97 98 99 |
# File 'lib/paperclip/aws/core.rb', line 96 def obtain_s3_instance_for() instances = (Thread.current[:paperclip_s3_instances] ||= {}) instances[] ||= s3_endpoint.present? ? ::Aws::S3.new(endpoint: s3_endpoint) : ::Aws::S3.new(endpoint: s3_endpoint) end |
#s3_credentials ⇒ Object
60 61 62 |
# File 'lib/paperclip/aws/core.rb', line 60 def s3_credentials # @s3_credentials ||= parse_credentials(@options[:s3_credentials]) end |
#s3_endpoint ⇒ Object
101 102 103 |
# File 'lib/paperclip/aws/core.rb', line 101 def s3_endpoint @s3_endpoint end |
#s3_host_alias ⇒ Object
71 72 73 74 |
# File 'lib/paperclip/aws/core.rb', line 71 def s3_host_alias @s3_host_alias = @options[:host_alias] || "#{s3_host_name}/#{bucket_name}" @s3_host_alias end |
#s3_host_name ⇒ Object
64 65 66 67 68 69 |
# File 'lib/paperclip/aws/core.rb', line 64 def s3_host_name host_name = @options[:s3_host_name] || "s3.amazonaws.com" # host_name = host_name.call(self) if host_name.is_a?(Proc) # # host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com" end |
#s3_interface ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/paperclip/aws/core.rb', line 88 def s3_interface @s3_interface ||= begin config = { } # :s3_endpoint => s3_host_name } obtain_s3_instance_for(config.merge(@s3_options)) end end |
#s3_protocol(style = default_style, with_colon = false) ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/paperclip/aws/core.rb', line 118 def s3_protocol(style = default_style, with_colon = false) protocol = @s3_protocol protocol = protocol.call(style, self) if protocol.respond_to?(:call) if with_colon && !protocol.empty? "#{protocol}:" else protocol.to_s end end |
#s3_url_options ⇒ Object
76 77 78 79 80 |
# File 'lib/paperclip/aws/core.rb', line 76 def = @options[:s3_url_options] || {} = .call(instance) if .respond_to?(:call) end |