lib/paperclip/storage/s3.rb in paperclip-4.1.1 vs lib/paperclip/storage/s3.rb in paperclip-4.2.0

- old
+ new

@@ -24,11 +24,11 @@ # secret_access_key: 456... # In which case, those access keys will be used in all environments. You can also # put your bucket name in this file, instead of adding it to the code directly. # This is useful when you want the same account but a different bucket for # development versus production. - # When using a Proc it provides a single parameter which is the attachment itself. A + # When using a Proc it provides a single parameter which is the attachment itself. A # method #instance is available on the attachment which will take you back to your # code. eg. # class User # has_attached_file :download, # :storage => :s3, @@ -49,11 +49,11 @@ # } # Or globally: # :s3_permissions => :private # # * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either - # 'http', 'https', or an empty string to generate scheme-less URLs. Defaults to 'http' + # 'http', 'https', or an empty string to generate protocol-relative URLs. Defaults to 'http' # when your :s3_permissions are :public_read (the default), and 'https' when your # :s3_permissions are anything else. # * +s3_headers+: A hash of headers or a Proc. You may specify a hash such as # {'Expires' => 1.year.from_now.httpdate}. If you use a Proc, headers are determined at # runtime. Paperclip will call that Proc with attachment as the only argument. @@ -100,10 +100,18 @@ # * +s3_storage_class+: If this option is set to # <tt>:reduced_redundancy</tt>, the object will be stored using Reduced # Redundancy Storage. RRS enables customers to reduce their # costs by storing non-critical, reproducible data at lower # levels of redundancy than Amazon S3's standard storage. + # + # You can set storage class on a per style bases by doing the following: + # :s3_storage_class => { + # :thumb => :reduced_reduncancy + # } + # Or globally: + # :s3_storage_class => :reduced_redundancy + module S3 def self.extended base begin require 'aws-sdk' rescue LoadError => e @@ -137,18 +145,18 @@ end @s3_metadata = @options[:s3_metadata] || {} @s3_headers = {} merge_s3_headers(@options[:s3_headers], @s3_headers, @s3_metadata) - @s3_headers[:storage_class] = @options[:s3_storage_class] if @options[:s3_storage_class] + @s3_storage_class = set_storage_class(@options[:s3_storage_class]) @s3_server_side_encryption = :aes256 if @options[:s3_server_side_encryption].blank? @s3_server_side_encryption = false end if @s3_server_side_encryption - @s3_server_side_encryption = @options[:s3_server_side_encryption].to_s.upcase + @s3_server_side_encryption = @options[:s3_server_side_encryption] 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" @@ -270,10 +278,15 @@ def set_permissions permissions permissions = { :default => permissions } unless permissions.respond_to?(:merge) permissions.merge :default => (permissions[:default] || :public_read) end + def set_storage_class(storage_class) + storage_class = {:default => storage_class} unless storage_class.respond_to?(:merge) + storage_class + end + def parse_credentials creds creds = creds.respond_to?('call') ? creds.call(self) : creds creds = find_credentials(creds).stringify_keys env = Object.const_defined?(:Rails) ? Rails.env : nil (creds[env] || creds).symbolize_keys @@ -293,10 +306,14 @@ s3_permissions = @s3_permissions[style] || @s3_permissions[:default] s3_permissions = s3_permissions.call(self, style) if s3_permissions.respond_to?(:call) s3_permissions end + def s3_storage_class(style = default_style) + @s3_storage_class[style] || @s3_storage_class[:default] + end + 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? @@ -318,9 +335,14 @@ acl = acl.call(self, style) if acl.respond_to?(:call) write_options = { :content_type => file.content_type, :acl => acl } + + # add storage class for this style if defined + storage_class = s3_storage_class(style) + write_options.merge!(:storage_class => storage_class) if storage_class + if @s3_server_side_encryption write_options[:server_side_encryption] = @s3_server_side_encryption end style_specific_options = styles[style]