Class: S3Provider
- Inherits:
-
Object
- Object
- S3Provider
- Defined in:
- app/models/s3_provider.rb
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
-
#acl ⇒ Object
Returns the value of attribute acl.
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#expiration_date ⇒ Object
Returns the value of attribute expiration_date.
-
#form_fields ⇒ Object
Returns the value of attribute form_fields.
-
#key ⇒ Object
Returns the value of attribute key.
-
#max_filesize ⇒ Object
Returns the value of attribute max_filesize.
-
#policy ⇒ Object
Returns the value of attribute policy.
-
#secret_access_key ⇒ Object
Returns the value of attribute secret_access_key.
-
#signature ⇒ Object
Returns the value of attribute signature.
Instance Method Summary collapse
- #foolbar ⇒ Object
- #form_action ⇒ Object
-
#initialize(options = {}) ⇒ S3Provider
constructor
A new instance of S3Provider.
- #script_data ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ S3Provider
Returns a new instance of S3Provider.
5 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 |
# File 'app/models/s3_provider.rb', line 5 def initialize( = {}) filename = "#{Rails.root}/config/amazon_s3.yml" config = YAML.load_file(filename) self.bucket = config["development"]['bucket'] self.access_key_id = config["development"]['access_key_id'] self.secret_access_key = config["development"]['secret_access_key'] self.key = [:key] || '' self.content_type = [:content_type] || '' self.acl = [:acl] || 'public-read' self.expiration_date = ([:expiration_date] || 10.hours).from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z') self.max_filesize = [:max_filesize] || 500.megabyte self.policy = Base64.encode64( "{'expiration': '#{self.expiration_date}', 'conditions': [ {'bucket': '#{self.bucket}'}, ['starts-with', '$key', '#{self.key}'], {'acl': '#{self.acl}'}, {'success_action_status': '201'}, ['content-length-range', 0, #{self.max_filesize}], ['starts-with', '$Content-Type', ''] ] }").gsub(/\n|\r/, '') self.signature = Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), self.secret_access_key, self.policy)).gsub("\n","") self.form_fields = %( <form action="#{form_action}" method="post" enctype="multipart/form-data" id="upload-form"> <input type="hidden" name="key" value="#{self.key}/${filename}"> <input type="hidden" name="AWSAccessKeyId" value="#{self.access_key_id}"> <input type="hidden" name="acl" value="#{self.policy}"> <input type="hidden" name="success_action_redirect" value="http://localhost/"> <input type="hidden" name="policy" value="#{self.policy}"> <input type="hidden" name="signature" value="#{self.signature}"> <input type="hidden" name="Content-Type" value="image/jpeg"> <input name="file" type="file"> <input type="submit" value="Upload File to S3"> </form> ) end |
Instance Attribute Details
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
2 3 4 |
# File 'app/models/s3_provider.rb', line 2 def access_key_id @access_key_id end |
#acl ⇒ Object
Returns the value of attribute acl.
2 3 4 |
# File 'app/models/s3_provider.rb', line 2 def acl @acl end |
#bucket ⇒ Object
Returns the value of attribute bucket.
2 3 4 |
# File 'app/models/s3_provider.rb', line 2 def bucket @bucket end |
#content_type ⇒ Object
Returns the value of attribute content_type.
2 3 4 |
# File 'app/models/s3_provider.rb', line 2 def content_type @content_type end |
#expiration_date ⇒ Object
Returns the value of attribute expiration_date.
2 3 4 |
# File 'app/models/s3_provider.rb', line 2 def expiration_date @expiration_date end |
#form_fields ⇒ Object
Returns the value of attribute form_fields.
2 3 4 |
# File 'app/models/s3_provider.rb', line 2 def form_fields @form_fields end |
#key ⇒ Object
Returns the value of attribute key.
2 3 4 |
# File 'app/models/s3_provider.rb', line 2 def key @key end |
#max_filesize ⇒ Object
Returns the value of attribute max_filesize.
2 3 4 |
# File 'app/models/s3_provider.rb', line 2 def max_filesize @max_filesize end |
#policy ⇒ Object
Returns the value of attribute policy.
2 3 4 |
# File 'app/models/s3_provider.rb', line 2 def policy @policy end |
#secret_access_key ⇒ Object
Returns the value of attribute secret_access_key.
2 3 4 |
# File 'app/models/s3_provider.rb', line 2 def secret_access_key @secret_access_key end |
#signature ⇒ Object
Returns the value of attribute signature.
2 3 4 |
# File 'app/models/s3_provider.rb', line 2 def signature @signature end |
Instance Method Details
#foolbar ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/models/s3_provider.rb', line 62 def %(<input type="hidden" name="key" value="#{self.key}/${filename}"> <input type="hidden" name="AWSAccessKeyId" value="#{self.access_key_id}"> <input type="hidden" name="acl" value="#{self.acl}"> <input type="hidden" name="policy" value="#{self.policy}"> <input type="hidden" name="signature" value="#{self.signature}"> <input type="hidden" name="success_action_status" value="201"> <input type="hidden" name="Content-Type" value="#{self.content_type}"> <input name="file" type="file" id="file_input" /> <input name="submit" value="Upload" type="submit" />) end |
#form_action ⇒ Object
49 50 51 |
# File 'app/models/s3_provider.rb', line 49 def form_action "https://#{self.bucket}.s3.amazonaws.com/" end |
#script_data ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'app/models/s3_provider.rb', line 52 def script_data %( "AWSAccessKeyId": #{s self.access_key_id}, "key": #{s self.key}, "acl": #{s self.acl}, "policy": #{url_encoded(%("#{self.policy}"), 2)}, "signature": #{s self.signature} ) end |