Last active
June 14, 2018 14:59
-
-
Save desheikh/1fd19cae06f92a9f8aadfd866730a1d2 to your computer and use it in GitHub Desktop.
Froala S3 Image upload using aws-sdk-ruby
This file contains hidden or 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 sdk signing doesn't allow for arbitrary form headers, issue #1399 | |
module Aws | |
module S3 | |
class PresignedPost | |
define_field(:requested_with, 'x-requested-with', starts_with: true) | |
end | |
end | |
end | |
class AmazonSignature | |
def data_hash | |
{ | |
policy: fields['policy'], | |
bucket: 'mybucket', | |
acl: 'public-read', | |
key_start: '/images/', | |
security_token: fields['x-amz-security-token'], | |
credential: fields['x-amz-credential'], | |
algorithm: fields['x-amz-algorithm'], | |
date: fields['x-amz-date'], | |
signature: fields['x-amz-signature'], | |
filename: fields['x-amz-meta-original-filename'] | |
} | |
end | |
def fields | |
credentials = Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_ACCESS_KEY_SECRET']) | |
@fields ||= Aws::S3::PresignedPost.new( | |
credentials, | |
'us-east-1', 'mybucket', | |
key_starts_with: '/images/', | |
acl: 'public-read', | |
content_length_range: 0..2097152, | |
success_action_status: '201', | |
requested_with_starts_with: 'xhr', | |
content_type_starts_with: 'image/', | |
metadata: { | |
'original-filename' => '${filename}' | |
} | |
).fields | |
end | |
end |
This file contains hidden or 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
imageUploadToS3: { | |
bucket: fields['bucket'], | |
region: 's3', | |
keyStart: fields['key_start'], | |
params: { | |
acl: fields['acl'], | |
policy: fields['policy'], | |
'x-amz-algorithm': fields['algorithm'], | |
'x-amz-credential': fields['credential'], | |
'x-amz-date': fields['date'], | |
'x-amz-signature': fields['signature'], | |
'x-amz-security-token': fields['security_token'], | |
'x-amz-meta-original-filename': fields['filename'] | |
} | |
} |
I figured it out, the security_token is only required if using temporary credentials so I excluded the :security_token => fields['x-amz-security-token'], line from the request and it worked
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am getting a :security_token => fields['x-amz-security-token'], returned as nil from the server. Are there any settings to setup through s3 to generate the token?