Class: S3Upload

Inherits:
Object
  • Object
show all
Defined in:
lib/s3-upload.rb,
lib/s3-upload/version.rb

Constant Summary collapse

Version =
VERSION = '0.1'

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ S3Upload

Returns a new instance of S3Upload.



16
17
18
19
20
21
22
23
24
# File 'lib/s3-upload.rb', line 16

def initialize(options = {})
  @options = options
  @options[:AWSAccessKeyId] ||= self.class.access_key_id
  @options[:AWSSecretAccessKey] ||= self.class.secret_access_key
  @options[:bucket] ||= self.class.bucket
  @options[:key_prefix] ||= self.class.default_key_prefix
  @options[:expiration_date] ||= Time.now + 60*60
  @options[:success_action_status] = '201'
end

Class Attribute Details

.access_key_idObject

Returns the value of attribute access_key_id.



10
11
12
# File 'lib/s3-upload.rb', line 10

def access_key_id
  @access_key_id
end

.bucketObject

Returns the value of attribute bucket.



12
13
14
# File 'lib/s3-upload.rb', line 12

def bucket
  @bucket
end

.default_key_prefixObject

Returns the value of attribute default_key_prefix.



13
14
15
# File 'lib/s3-upload.rb', line 13

def default_key_prefix
  @default_key_prefix
end

.secret_access_keyObject

Returns the value of attribute secret_access_key.



11
12
13
# File 'lib/s3-upload.rb', line 11

def secret_access_key
  @secret_access_key
end

Instance Method Details

#as_json(options = {}) ⇒ Object



65
66
67
68
69
# File 'lib/s3-upload.rb', line 65

def as_json(options = {})
  { :url => self.url,
    :file_param => self.file_param,
    :params => self.params }
end

#file_paramObject



30
31
32
# File 'lib/s3-upload.rb', line 30

def file_param
  'file'
end

#paramsObject



57
58
59
60
61
62
63
# File 'lib/s3-upload.rb', line 57

def params
  { :success_action_status => @options[:success_action_status],
    :AWSAccessKeyId => @options[:AWSAccessKeyId],
    :key => "#{@options[:key_prefix]}${filename}",
    :policy => self.policy,
    :signature => self.signature }
end

#policyObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/s3-upload.rb', line 34

def policy
  conditions = []
  conditions << { :bucket => @options[:bucket].to_s }
  conditions << ['starts-with', '$key', @options[:key_prefix].to_s]
  conditions << { :success_action_redirect => @options[:success_action_redirect].to_s } if @options[:success_action_redirect]
  conditions << { :success_action_status => @options[:success_action_status].to_s } if @options[:success_action_status]
  conditions << ['starts-with', '$Filename', '']
  if @options[:meta_fields]
    @options[:meta_fields].sort.each do |meta_field|
      conditions << ['starts-with', "$#{meta_field}", '']
    end
  end
  Base64.encode64(
  "{ 
     'expiration': '#{self.class.expiration_date(@options[:expiration_date])}',
     'conditions': #{conditions.to_json}
   }").gsub(/\n|\r/, '')
end

#signatureObject



53
54
55
# File 'lib/s3-upload.rb', line 53

def signature
  Base64.encode64(HMAC::SHA1.digest(@options[:AWSSecretAccessKey],policy)).strip
end

#urlObject



26
27
28
# File 'lib/s3-upload.rb', line 26

def url
  "http://#{@options[:bucket]}.s3.amazonaws.com/"
end