Skip to content

Instantly share code, notes, and snippets.

@desheikh
Last active June 14, 2018 14:59
Show Gist options
  • Save desheikh/1fd19cae06f92a9f8aadfd866730a1d2 to your computer and use it in GitHub Desktop.
Save desheikh/1fd19cae06f92a9f8aadfd866730a1d2 to your computer and use it in GitHub Desktop.
Froala S3 Image upload using aws-sdk-ruby
# 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
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']
}
}
@ryanbelke
Copy link

ryanbelke commented Apr 15, 2017

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?

module Aws
  module S3
    class PresignedPost
      define_field(:requested_with, 'x-requested-with', starts_with: true)
    end
  end
end

class AmazonSignature
  def self.fields
    credentials = Aws::Credentials.new(ENV['s3_access_key_id'], ENV['s3_secret_access_key'])
    @fields ||= Aws::S3::PresignedPost.new(
        credentials,
        'us-west-2', 'mybucket',
        key_starts_with: '/articles/',
        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

  def self.data_hash
    {
        :policy => fields['policy'],
        :bucket => 'mybucket',
        :acl => 'public-read',
        :key_start => '/articles/',
        :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

@ryanbelke
Copy link

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