Class: Milton::Storage::S3File
- Inherits:
-
StoredFile
- Object
- StoredFile
- Milton::Storage::S3File
- Defined in:
- lib/milton/storage/s3_file.rb
Instance Attribute Summary
Attributes inherited from StoredFile
Instance Method Summary collapse
-
#copy(destination) ⇒ Object
Copies this file to the given location on disk.
- #destroy ⇒ Object
- #dirname ⇒ Object
- #exists? ⇒ Boolean
- #mime_type ⇒ Object
- #path ⇒ Object
-
#signature(expires_at = nil) ⇒ Object
Generates a signature for passing authorization for this file on to another user without having to proxy the file.
-
#signed_url(expires_at = nil) ⇒ Object
Generates a signed url to this resource on S3.
- #store(source) ⇒ Object
Methods inherited from StoredFile
adapter, #clone, create, #initialize, sanitize_filename
Constructor Details
This class inherits a constructor from Milton::Storage::StoredFile
Instance Method Details
#copy(destination) ⇒ Object
Copies this file to the given location on disk. Note that this copies to a LOCAL location, not to another place on S3!
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/milton/storage/s3_file.rb', line 39 def copy(destination) Milton.log "copying #{path} to #{destination}" s3 = RightAws::S3Interface.new([:storage_options][:access_key_id], [:storage_options][:secret_access_key], :logger => Rails.logger) file = File.new(destination, 'wb') # stream the download as opposed to downloading the whole thing and reading # it all into memory at once since it might be gigantic... s3.get([:storage_options][:bucket], key) { |chunk| file.write(chunk) } file.close end |
#destroy ⇒ Object
32 33 34 35 |
# File 'lib/milton/storage/s3_file.rb', line 32 def destroy Milton.log "destroying #{path}" bucket.key(key).try(:delete) end |
#dirname ⇒ Object
19 20 21 |
# File 'lib/milton/storage/s3_file.rb', line 19 def dirname id end |
#exists? ⇒ Boolean
23 24 25 |
# File 'lib/milton/storage/s3_file.rb', line 23 def exists? bucket.key(key).exists? end |
#mime_type ⇒ Object
51 52 53 |
# File 'lib/milton/storage/s3_file.rb', line 51 def mime_type # TODO: implement end |
#path ⇒ Object
15 16 17 |
# File 'lib/milton/storage/s3_file.rb', line 15 def path "http://#{[:storage_options][:bucket]}.s3.amazonaws.com/#{key}" end |
#signature(expires_at = nil) ⇒ Object
Generates a signature for passing authorization for this file on to another user without having to proxy the file.
See docs.amazonwebservices.com/AmazonS3/latest/index.html?RESTAuthentication.html
Optionally pass expires_at to make the signature valid only until given expiration date/time – useful for temporary secure access to files.
72 73 74 75 76 77 78 |
# File 'lib/milton/storage/s3_file.rb', line 72 def signature(expires_at=nil) CGI.escape(Base64.encode64(OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), [:storage_options][:secret_access_key], "GET\n\n\n#{expires_at ? expires_at.to_i : ''}\n/#{[:storage_options][:bucket]}/#{key}" )).chomp.gsub(/\n/, '')) end |
#signed_url(expires_at = nil) ⇒ Object
Generates a signed url to this resource on S3.
See doc for signature.
58 59 60 61 62 |
# File 'lib/milton/storage/s3_file.rb', line 58 def signed_url(expires_at=nil) "#{path}?AWSAccessKeyId=#{[:storage_options][:access_key_id]}" + (expires_at ? "&Expires=#{expires_at.to_i}" : '') + "&Signature=#{signature(expires_at)}" end |