Class: S3Provider

Inherits:
Object
  • Object
show all
Defined in:
app/models/s3_provider.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  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             = options[:key] || ''
  self.content_type    = options[:content_type] || ''
  self.acl             = options[:acl] || 'public-read'
  self.expiration_date = (options[:expiration_date] || 10.hours).from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
  self.max_filesize    = options[: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_idObject

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

#aclObject

Returns the value of attribute acl.



2
3
4
# File 'app/models/s3_provider.rb', line 2

def acl
  @acl
end

#bucketObject

Returns the value of attribute bucket.



2
3
4
# File 'app/models/s3_provider.rb', line 2

def bucket
  @bucket
end

#content_typeObject

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_dateObject

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_fieldsObject

Returns the value of attribute form_fields.



2
3
4
# File 'app/models/s3_provider.rb', line 2

def form_fields
  @form_fields
end

#keyObject

Returns the value of attribute key.



2
3
4
# File 'app/models/s3_provider.rb', line 2

def key
  @key
end

#max_filesizeObject

Returns the value of attribute max_filesize.



2
3
4
# File 'app/models/s3_provider.rb', line 2

def max_filesize
  @max_filesize
end

#policyObject

Returns the value of attribute policy.



2
3
4
# File 'app/models/s3_provider.rb', line 2

def policy
  @policy
end

#secret_access_keyObject

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

#signatureObject

Returns the value of attribute signature.



2
3
4
# File 'app/models/s3_provider.rb', line 2

def signature
  @signature
end

Instance Method Details

#foolbarObject



62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/s3_provider.rb', line 62

def foolbar
   %(<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_actionObject



49
50
51
# File 'app/models/s3_provider.rb', line 49

def form_action
  "https://#{self.bucket}.s3.amazonaws.com/"
end

#script_dataObject



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