Skip to content

Instantly share code, notes, and snippets.

@evnpr
Created December 12, 2012 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evnpr/4263577 to your computer and use it in GitHub Desktop.
Save evnpr/4263577 to your computer and use it in GitHub Desktop.
Direct Upload Amazon S3
<%
require 'base64'
require 'openssl'
require 'digest/sha1'
secret_key = "5J0VAOMMkrh5mYEJN4WuybZXfLFQMI"
policy_document = '{"expiration": "2013-01-01T00:00:00Z",
"conditions": [
{"bucket": "evanpurnama"},
["starts-with", "$key", ""],
{"acl": "private"},
{"success_action_redirect": "http://localhost:3000/"},
["starts-with", "$Content-Type", ""],
["content-length-range", 0, 1048576]
]
}'
policy = Base64.encode64(policy_document).gsub("\n","")
signature = Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest::Digest.new('sha1'),
secret_key, policy)
).gsub("\n","")
%>
<html>
<head>
<title>S3 POST Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="https://evanpurnama.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="${filename}">
<input type="hidden" name="AWSAccessKeyId" value="AKIAIVZGI7PPMYYSUFCQ">
<input type="hidden" name="acl" value="private">
<input type="hidden" name="success_action_redirect" value="http://localhost:3000/">
<input type="hidden" name="policy" value="<%= policy %>">
<input type="hidden" name="signature" value="<%= signature %>">
<input type="hidden" name="Content-Type" value="image/jpeg">
<!-- Include any additional input fields here -->
File to upload to S3:
<input name="file" type="file">
<br>
<input type="submit" value="Upload File to S3">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment