Skip to content

Instantly share code, notes, and snippets.

@arekt
Forked from ewryan/s3_query_url.rb
Last active Aug 29, 2015
Embed
What would you like to do?
#Based off camwest's gist at https://gist.github.com/363958
#Using to_s instead of extending String
require 'openssl'
require 'cgi'
class S3QueryURL
def initialize(bucket, path, options = {})
@access_key_id = options[:access_key_id]
@secret_access_key = options[:secret_access_key]
@bucket = bucket
@expires = (Time.now.utc + Integer(options[:expires_in])).to_i
@path = path
end
def to_s
"https://#{@bucket}.s3.amazonaws.com/#{@path}?#{query_string}"
end
private
def query_string
"AWSAccessKeyId=#{@access_key_id}&Expires=#{@expires}&Signature=#{encoded_canonical}"
end
def encoded_canonical
digest = OpenSSL::Digest::Digest.new('sha1')
b64_hmac = [OpenSSL::HMAC.digest(digest, @secret_access_key, canonical_string)].pack("m").strip
CGI.escape( b64_hmac )
end
def canonical_string
"GET\n\n\n#{@expires}\n/#{@bucket}/#{@path}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment