Created
August 27, 2013 23:12
-
-
Save vitaly/6360241 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env ruby | |
| require 'base64' | |
| require 'openssl' | |
| require 'digest/sha1' | |
| def ask(prompt) | |
| STDOUT.print "#{prompt}: " | |
| readline.strip | |
| end | |
| ACCESS_KEY = ENV['AWS_ACCESS_KEY'] or raise 'no AWS_ACCESS_KEY' | |
| SECRET_KEY = ENV['AWS_SECRET_ACCESS_KEY'] or raise 'no AWS_SECRET_ACCESS_KEY' | |
| BUCKET = ENV['S3_BUCKET'] || ask(:S3_BUCKET) | |
| UPLOAD_PATH = ENV['S3_UPLOAD_PATH'] || ask(:S3_UPLOAD_PATH) | |
| FILE = ENV['S3_FILE_NAME'] || ask(:S3_FILE_NAME) | |
| REDIRECT = "http://#{BUCKET}.s3.amazonaws.com/#{UPLOAD_PATH}/#{FILE}" | |
| POLICY = <<-_ | |
| {"expiration": "2014-01-01T00:00:00Z", | |
| "conditions": [ | |
| {"bucket": "#{BUCKET}"}, | |
| ["starts-with", "$key", "#{UPLOAD_PATH}/"], | |
| {"acl": "public-read"}, | |
| {"success_action_redirect": "#{REDIRECT}"}, | |
| ["starts-with", "$Content-Type", ""], | |
| ["content-length-range", 0, 1048576] | |
| ] | |
| } | |
| _ | |
| policy = Base64.encode64(POLICY.strip).gsub("\n","") | |
| signature = Base64.encode64( | |
| OpenSSL::HMAC.digest( | |
| OpenSSL::Digest::Digest.new('sha1'), | |
| SECRET_KEY, policy) | |
| ).gsub("\n","") | |
| File.open('form.html', 'w') do |file| | |
| file.write <<-html | |
| <html> | |
| <head> | |
| <title>S3 POST Form</title> | |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
| </head> | |
| <body> | |
| <table border=1> | |
| <tr><td>BUCKET</td><td>#{BUCKET}</td></tr> | |
| <tr><td>UPLOAD_PATH</td><td>#{UPLOAD_PATH}</td></tr> | |
| <tr><td>FILE</td><td>#{FILE}</td></tr> | |
| <tr><td>REDIRECT</td><td>#{REDIRECT}</td></tr> | |
| </table> | |
| <br/> | |
| <form action="https://#{BUCKET}.s3.amazonaws.com/" method="post" enctype="multipart/form-data"> | |
| <input type="hidden" name="key" value="#{UPLOAD_PATH}/#{FILE}"> | |
| <input type="hidden" name="AWSAccessKeyId" value="#{ACCESS_KEY}"> | |
| <input type="hidden" name="acl" value="public-read"> | |
| <input type="hidden" name="success_action_redirect" value="#{REDIRECT}"> | |
| <input type="hidden" name="policy" value="#{policy}"> | |
| <input type="hidden" name="signature" value="#{signature}"> | |
| <input type="hidden" name="Content-Type" value="image/jpeg"> | |
| File: <input name="file" type="file"> | |
| <br> | |
| <input type="submit" value="Upload File to S3"> | |
| </form> | |
| </body> | |
| </html> | |
| html | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment