Class: Anaconda::S3Uploader
- Inherits:
-
Object
- Object
- Anaconda::S3Uploader
- Defined in:
- lib/anaconda/s3_uploader.rb
Instance Method Summary collapse
- #base_key ⇒ Object
- #fields ⇒ Object
- #form_options ⇒ Object
-
#initialize(options) ⇒ S3Uploader
constructor
A new instance of S3Uploader.
- #key ⇒ Object
- #policy ⇒ Object
- #policy_data ⇒ Object
- #signature ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(options) ⇒ S3Uploader
Returns a new instance of S3Uploader.
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/anaconda/s3_uploader.rb', line 3 def initialize() @options = .reverse_merge( id: "fileupload", aws_access_key_id: Anaconda.aws[:aws_access_key], aws_secret_access_key: Anaconda.aws[:aws_secret_key], aws_bucket: Anaconda.aws[:aws_bucket], aws_endpoint: Anaconda.aws[:aws_endpoint], acl: "public-read", expiration: 10.hours.from_now.utc, max_file_size: 500.megabytes, as: "file" ) end |
Instance Method Details
#base_key ⇒ Object
46 47 48 |
# File 'lib/anaconda/s3_uploader.rb', line 46 def base_key @options[:base_key] end |
#fields ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/anaconda/s3_uploader.rb', line 31 def fields { :key => key, :acl => @options[:acl], "Content-Type" => "application/octet-stream", :policy => policy, :signature => signature, "AWSAccessKeyId" => @options[:aws_access_key_id], } end |
#form_options ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/anaconda/s3_uploader.rb', line 17 def { id: @options[:id], method: "post", authenticity_token: false, multipart: true, data: { post: @options[:post], as: @options[:as], base_key: base_key } } end |
#key ⇒ Object
42 43 44 |
# File 'lib/anaconda/s3_uploader.rb', line 42 def key @key ||= "#{base_key}/${filename}" end |
#policy ⇒ Object
54 55 56 |
# File 'lib/anaconda/s3_uploader.rb', line 54 def policy Base64.encode64(policy_data.to_json).gsub("\n", "") end |
#policy_data ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/anaconda/s3_uploader.rb', line 58 def policy_data { expiration: @options[:expiration], conditions: [ #["starts-with", "$utf8", ""], ["starts-with", "$key", base_key], ["starts-with", "$Content-Type", ""], ["content-length-range", 1, @options[:max_file_size]], {bucket: @options[:aws_bucket]}, {acl: @options[:acl]} ] } end |
#signature ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/anaconda/s3_uploader.rb', line 72 def signature Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest.new('sha1'), @options[:aws_secret_access_key], policy ) ).gsub("\n", "") end |
#url ⇒ Object
50 51 52 |
# File 'lib/anaconda/s3_uploader.rb', line 50 def url "https://#{@options[:aws_endpoint]}/" end |