Skip to content

Instantly share code, notes, and snippets.

@DmZ
Created November 13, 2012 19:57
Show Gist options
  • Save DmZ/4067998 to your computer and use it in GitHub Desktop.
Save DmZ/4067998 to your computer and use it in GitHub Desktop.
Simple S3 signing for expiring URLs (Ruby)
#!/usr/bin/env ruby
require 'time'
require 'uri'
require 'cgi'
require 'base64'
require 'openssl'
expires = Time.now().to_i + Integer(ARGV[0])
url = URI.parse(ARGV[1])
access_key = ENV["AWS_ACCESS_KEY_ID"]
secret_key = ENV["AWS_SECRET_ACCESS_KEY"]
h = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new,
secret_key,
"GET\n\n\n#{expires}\n#{url.path}")
sign = CGI::escape(Base64.encode64(h).strip)
print "#{ARGV[1]}?AWSAccessKeyId=#{access_key}&Expires=#{expires}&Signature=#{sign}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment