Class: FroalaEditorSDK::S3
- Inherits:
-
Object
- Object
- FroalaEditorSDK::S3
- Defined in:
- lib/froala-editor-sdk/s3.rb
Overview
Uploads files to S3/AWS
Class Method Summary collapse
-
.data_hash(options = nil) ⇒ Object
- Makes all the request in order and returns AWS hash response Params:
options
-
Configuration params to generate the AWS response.
- Makes all the request in order and returns AWS hash response Params:
-
.getXamzCredential(options = nil) ⇒ Object
- Builds the amazon credential by appending access key, x-amz-date, region and ‘s3/aws4_request’ Params:
options
-
Configuration params to generate the AWS response.
- Builds the amazon credential by appending access key, x-amz-date, region and ‘s3/aws4_request’ Params:
-
.policy(options = nil) ⇒ Object
Encodes to Base64 the policy data and replaces new lines chars.
-
.policy_data(options = nil) ⇒ Object
Sets policy params, bucket that will be used max file size and other params.
-
.sign(key, data) ⇒ Object
- Builds a HMAC-SHA256 digest using key and data Params:
key
- Key to use for creating the digest
data
-
Data to be used for creating the digest.
- Key to use for creating the digest
- Builds a HMAC-SHA256 digest using key and data Params:
-
.signature(options = nil) ⇒ Object
Builds a signature based on the options.
Class Method Details
.data_hash(options = nil) ⇒ Object
Makes all the request in order and returns AWS hash response Params:
options
-
Configuration params to generate the AWS response.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/froala-editor-sdk/s3.rb', line 74 def self.data_hash ( = nil) [:region] = 'us-east-1' if [:region].nil? || [:region] == 's3' [:'date-string'] = Time.now.strftime("%Y%m%d") [:'x-amz-algorithm'] = "AWS4-HMAC-SHA256" [:'x-amz-credential'] = self.getXamzCredential() [:'x-amz-date'] = [:'date-string'] + "T000000Z" { :bucket => [:bucket], # Upload bucket :region => [:region] != 'us-east-1' ? "s3-#{[:region]}" : 's3', # Upload region :keyStart => [:keyStart], # Start key/folder :params => { :acl => [:acl], # ACL property 'public-read' :policy => self.policy(), # Defined policy :'x-amz-algorithm' => [:'x-amz-algorithm'], # Encrytion Algorithm :'x-amz-credential' => [:'x-amz-credential'], # Encrypted Credentials :'x-amz-date' => [:'x-amz-date'], # Current Date :'x-amz-signature' => self.signature(), # Defined signature } } end |
.getXamzCredential(options = nil) ⇒ Object
Builds the amazon credential by appending access key, x-amz-date, region and ‘s3/aws4_request’ Params:
options
-
Configuration params to generate the AWS response
67 68 69 |
# File 'lib/froala-editor-sdk/s3.rb', line 67 def self.getXamzCredential( = nil) "#{[:accessKey]}#{"/"}#{[:'date-string']}#{"/"}#{[:region]}#{"/"}#{"s3/aws4_request"}" end |
.policy(options = nil) ⇒ Object
Encodes to Base64 the policy data and replaces new lines chars. Params:
options
-
The configuration params that are needed to compute the signature.
40 41 42 |
# File 'lib/froala-editor-sdk/s3.rb', line 40 def self.policy ( = nil) Base64.encode64(self.policy_data().to_json).gsub("\n", "") end |
.policy_data(options = nil) ⇒ Object
Sets policy params, bucket that will be used max file size and other params. Params:
options
-
Configuration params that are needed to set the policy
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/froala-editor-sdk/s3.rb', line 47 def self.policy_data ( = nil) { expiration: 10.hours.from_now.utc.iso8601, conditions: [ ["starts-with", "$key", [:keyStart]], # Start key/folder ["starts-with", "$Content-type", ""], # Content type {"x-requested-with": "xhr"}, # Request type {"x-amz-algorithm": [:'x-amz-algorithm']}, # Encrytion Algorithm {"x-amz-date": [:'x-amz-date']}, # Current Date {"x-amz-credential": [:'x-amz-credential']}, # Encrypted Credentials {bucket: [:bucket]}, # Bucket name {acl: [:acl]}, # ACL property {success_action_status: "201"} # Response status 201 'file created' ] } end |
.sign(key, data) ⇒ Object
Builds a HMAC-SHA256 digest using key and data Params:
key
-
Key to use for creating the digest
data
-
Data to be used for creating the digest
33 34 35 |
# File 'lib/froala-editor-sdk/s3.rb', line 33 def self.sign(key, data) OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, key, data.force_encoding(Encoding::UTF_8)) end |
.signature(options = nil) ⇒ Object
Builds a signature based on the options. Params:
options
-
The configuration params that are needed to compute the signature.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/froala-editor-sdk/s3.rb', line 9 def self.signature ( = nil) OpenSSL::HMAC.hexdigest( "SHA256", self.sign( self.sign( self.sign( self.sign( ("AWS4" + [:secretKey]).force_encoding(Encoding::UTF_8), [:'date-string'] ), [:region] ), "s3" ), "aws4_request" ), self.policy() ) end |