Module: Lipsiadmin::Attachment::Storage::S3
- Defined in:
- lib/data_base/attachment/storage.rb
Overview
Amazon’s S3 file hosting service is a scalable, easy place to store files for distribution. You can find out more about it at aws.amazon.com/s3 There are a few S3-specific options for has_attached_file:
-
s3_credentials: Takes a path, a File, or a Hash. The path (or File) must point to a YAML file containing theaccess_key_idandsecret_access_keythat Amazon gives you. You can ‘environment-space’ this just like you do to your database.yml file, so different environments can use different accounts:development: access_key_id: 123... secret_access_key: 123... test: access_key_id: abc... secret_access_key: abc... production: access_key_id: 456... secret_access_key: 456...This is not required, however, and the file may simply look like this:
access_key_id: 456... 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.
-
s3_permissions: This is a String that should be one of the “canned” access policies that S3 provides (more information can be found here: docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html#RESTCannedAccessPolicies) The default for Attachment is “public-read”. -
s3_protocol: The protocol for the URLs generated to your S3 assets. Can be either ‘http’ or ‘https’. 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 such as => 1.year.from_now.httpdate -
bucket: This is the name of the S3 bucket that will store your files. Remember that the bucket must be unique across all of Amazon S3. If the bucket does not exist Attachment will attempt to create it. The bucket name will not be interpolated. -
url: There are two options for the S3 url. You can choose to have the bucket’s name placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket). Normally, this won’t matter in the slightest and you can leave the default (which is path-style, or :s3_path_url). But in some cases paths don’t work and you need to use the domain-style (:s3_domain_url). Anything else here will be treated like path-style. -
path: This is the key under the bucket in which the file will be stored. The URL will be constructed from the bucket and the path. This is what you will want to interpolate. Keys should be unique, like filenames, and despite the fact that S3 (strictly speaking) does not support directories, you can still use a / to separate parts of your file name.
Class Method Summary collapse
Instance Method Summary collapse
- #bucket_name ⇒ Object
- #exists?(style = default_style) ⇒ Boolean
-
#flush_deletes ⇒ Object
:nodoc:.
-
#flush_writes ⇒ Object
:nodoc:.
- #parse_credentials(creds) ⇒ Object
- #s3 ⇒ Object
- #s3_bucket ⇒ Object
- #s3_protocol ⇒ Object
-
#to_file(style = default_style) ⇒ Object
(also: #to_io)
Returns representation of the data of the file assigned to the given style, in the format most representative of the current storage.
Class Method Details
.extended(base) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/data_base/attachment/storage.rb', line 122 def self.extended base require 'right_aws' base.instance_eval do @s3_credentials = parse_credentials(@options[:s3_credentials]) @bucket = @options[:bucket] || @s3_credentials[:bucket] @s3_options = @options[:s3_options] || {} @s3_permissions = @options[:s3_permissions] || 'public-read' @s3_protocol = @options[:s3_protocol] || (@s3_permissions == 'public-read' ? 'http' : 'https') @s3_headers = @options[:s3_headers] || {} @url = ":s3_path_url" unless @url.to_s.match(/^:s3.*url$/) end base.class.interpolations[:s3_path_url] = lambda do |, style| "#{.s3_protocol}://s3.amazonaws.com/#{.bucket_name}/#{.path(style).gsub(%r{^/}, "")}" end base.class.interpolations[:s3_domain_url] = lambda do |, style| "#{.s3_protocol}://#{.bucket_name}.s3.amazonaws.com/#{.path(style).gsub(%r{^/}, "")}" end ActiveRecord::Base.logger.info("[Attachment] S3 Storage Initalized.") end |
Instance Method Details
#bucket_name ⇒ Object
152 153 154 |
# File 'lib/data_base/attachment/storage.rb', line 152 def bucket_name @bucket end |
#exists?(style = default_style) ⇒ Boolean
161 162 163 |
# File 'lib/data_base/attachment/storage.rb', line 161 def exists?(style = default_style) s3_bucket.key(path(style)) ? true : false end |
#flush_deletes ⇒ Object
:nodoc:
191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/data_base/attachment/storage.rb', line 191 def flush_deletes #:nodoc: logger.info("[Attachment] Writing files for #{name}") @queued_for_delete.each do |path| begin logger.info("[Attachment] -> #{path}") if file = s3_bucket.key(path) file.delete end rescue RightAws::AwsError # Ignore this. end end @queued_for_delete = [] end |
#flush_writes ⇒ Object
:nodoc:
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/data_base/attachment/storage.rb', line 176 def flush_writes #:nodoc: logger.info("[Attachment] Writing files for #{name}") @queued_for_write.each do |style, file| begin logger.info("[Attachment] -> #{path(style)}") key = s3_bucket.key(path(style)) key.data = file key.put(nil, @s3_permissions, {'Content-type' => instance_read(:content_type)}.merge(@s3_headers)) rescue RightAws::AwsError => e raise end end @queued_for_write = {} end |
#parse_credentials(creds) ⇒ Object
156 157 158 159 |
# File 'lib/data_base/attachment/storage.rb', line 156 def parse_credentials(creds) creds = find_credentials(creds).stringify_keys (creds[ENV['RAILS_ENV']] || creds).symbolize_keys end |
#s3 ⇒ Object
142 143 144 145 146 |
# File 'lib/data_base/attachment/storage.rb', line 142 def s3 @s3 ||= RightAws::S3.new(@s3_credentials[:access_key_id], @s3_credentials[:secret_access_key], @s3_options) end |
#s3_bucket ⇒ Object
148 149 150 |
# File 'lib/data_base/attachment/storage.rb', line 148 def s3_bucket @s3_bucket ||= s3.bucket(@bucket, true, @s3_permissions) end |
#s3_protocol ⇒ Object
165 166 167 |
# File 'lib/data_base/attachment/storage.rb', line 165 def s3_protocol @s3_protocol end |
#to_file(style = default_style) ⇒ Object Also known as: to_io
Returns representation of the data of the file assigned to the given style, in the format most representative of the current storage.
171 172 173 |
# File 'lib/data_base/attachment/storage.rb', line 171 def to_file(style = default_style) @queued_for_write[style] || s3_bucket.key(path(style)) end |