Last active
June 26, 2019 05:29
-
-
Save stefanneculai/deed108fad534d0db3ff to your computer and use it in GitHub Desktop.
Amazon Signature Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AmazonSignature | |
extend self | |
def signature | |
Base64.encode64( | |
OpenSSL::HMAC.digest( | |
OpenSSL::Digest.new('sha1'), | |
AWS_CONFIG['secret_access_key'], self.policy | |
) | |
).gsub("\n", "") | |
end | |
def policy | |
Base64.encode64(self.policy_data.to_json).gsub("\n", "") | |
end | |
def policy_data | |
{ | |
expiration: 10.hours.from_now.utc.iso8601, | |
conditions: [ | |
["starts-with", "$key", AWS_CONFIG['key_start']], | |
["starts-with", "$x-requested-with", "xhr"], | |
["content-length-range", 0, 20.megabytes], | |
["starts-with", "$content-type", ""], | |
{bucket: AWS_CONFIG['bucket']}, | |
{acl: AWS_CONFIG['acl']}, | |
{success_action_status: "201"} | |
] | |
} | |
end | |
def data_hash | |
{ | |
:bucket => AWS_CONFIG['bucket'], | |
:region => AWS_CONFIG['region'], | |
:keyStart => AWS_CONFIG['key_start'], | |
:params => { | |
:acl => AWS_CONFIG['acl'], | |
:AWSAccessKeyId => ENV['access_key_id'], | |
:signature => self.signature, | |
:policy => self.policy | |
} | |
} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWS_CONFIG = { | |
'access_key_id' => YOUR_ACCESS_KEY, | |
'secret_access_key' => YOUR_SECRET_ACCESS_KEY, | |
'bucket' => 'froala', | |
'acl' => 'public-read', | |
'key_start' => 'uploads/', | |
'region' => 's3' # For other regions than us-east-1, use s3-region. E.g.: s3-eu-west-1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> | |
<CORSRule> | |
<AllowedOrigin>REPLACE_THIS_WITH_THE_URL_OF_THE_PAGE_WHERE_YOU_USE_THE_EDITOR</AllowedOrigin> | |
<AllowedMethod>GET</AllowedMethod> | |
<AllowedMethod>POST</AllowedMethod> | |
<AllowedMethod>PUT</AllowedMethod> | |
<MaxAgeSeconds>3000</MaxAgeSeconds> | |
<AllowedHeader>*</AllowedHeader> | |
</CORSRule> | |
</CORSConfiguration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to fix the "preflight request" error -
Change:
region: 's3', // Change the region if it is different
To your region, which for me was:
region: 's3-us-west-2', // Change the region if it is different
To find the correct region, since I tried just "us-west-2" which didn't work, I had to look at URL for a previously uploaded image in that bucket, where I found the full region name; "s3-us-west-2"
Hope that helps!