Class: Milton::Storage::S3File

Inherits:
StoredFile show all
Defined in:
lib/milton/storage/s3_file.rb

Instance Attribute Summary

Attributes inherited from StoredFile

#filename, #id, #options

Instance Method Summary collapse

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(options[:storage_options][:access_key_id], options[: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(options[:storage_options][:bucket], key) { |chunk| file.write(chunk) }
  file.close
end

#destroyObject



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

#dirnameObject



19
20
21
# File 'lib/milton/storage/s3_file.rb', line 19

def dirname
  id
end

#exists?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/milton/storage/s3_file.rb', line 23

def exists?
  bucket.key(key).exists?
end

#mime_typeObject



51
52
53
# File 'lib/milton/storage/s3_file.rb', line 51

def mime_type
  # TODO: implement
end

#pathObject



15
16
17
# File 'lib/milton/storage/s3_file.rb', line 15

def path
  "http://#{options[: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'),
    options[:storage_options][:secret_access_key],
    "GET\n\n\n#{expires_at ? expires_at.to_i : ''}\n/#{options[: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=#{options[:storage_options][:access_key_id]}" +
  (expires_at ? "&Expires=#{expires_at.to_i}" : '') +
  "&Signature=#{signature(expires_at)}"
end

#store(source) ⇒ Object



27
28
29
30
# File 'lib/milton/storage/s3_file.rb', line 27

def store(source)
  Milton.log "storing #{source} to #{path} (#{options[:storage_options][:permissions]})"
  bucket.put(key, File.open(source), {}, options[:storage_options][:permissions])
end