Created
October 5, 2014 20:36
-
-
Save xrimpy/e8a4903fdadf9a0387cd to your computer and use it in GitHub Desktop.
Upload directly to S3
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
#application_helper.rb | |
def s3_upload_policy_document(filename) | |
ret = {"expiration" => 30.minutes.from_now.utc.xmlschema, | |
"conditions" => [ | |
{"bucket" => ENV['S3BUCKET']}, | |
["starts-with", "$key", filename], | |
{"acl" => "private"}, | |
{"success_action_status" => "200"}, | |
["content-length-range", 0, 1048576] | |
] | |
} | |
Base64.encode64(ret.to_json).gsub(/\n/,'') | |
end | |
def s3_upload_signature(policy) | |
signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), ENV['S3SECRET'], policy)).gsub("\n","") | |
end | |
#CODE FOR THE FORM | |
<% filename = "igot5onit.jpg" %> | |
<form id="AWS" action="https://whenwemarry-uploads.s3.amazonaws.com" method="post" enctype="multipart/form-data"> | |
<%= hidden_field_tag(:key, filename ) %> | |
<%= hidden_field_tag(:AWSAccessKeyId, ENV['S3KEY']) %> | |
<%= hidden_field_tag(:acl, "private") %> | |
<%= hidden_field_tag(:success_action_status, "200") %> | |
<%= hidden_field_tag(:policy, s3_upload_policy_document(filename)) %> | |
<%= hidden_field_tag(:signature, s3_upload_signature(s3_upload_policy_document(filename))) %> | |
<input id="ffileupload" type="file" name="file" multiple> | |
</form> | |
<div id="sub" style="border: solid 1px #CCC">SUBMIT</div> | |
<script type="text/javascript"> | |
$('#sub').click( function() | |
{ | |
$('#AWS').submit(); | |
}); | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment