Skip to content

Instantly share code, notes, and snippets.

@manur
Forked from mcansky/gist:3434417
Created February 11, 2014 21:22
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 manur/8944463 to your computer and use it in GitHub Desktop.
Save manur/8944463 to your computer and use it in GitHub Desktop.
# encoding : utf-8
require 'openssl'
require 'digest/sha1'
require 'base64'
module Aws
extend self
def signed_url(path, expire_date)
digest = OpenSSL::Digest::Digest.new('sha1')
can_string = "GET\n\n\n#{expire_date}\n/#{S3_BUCKET}/#{path}"
hmac = OpenSSL::HMAC.digest(digest, S3_SECRET_ACCESS_KEY, can_string)
signature = URI.escape(Base64.encode64(hmac).strip).encode_signs
"https://s3.amazonaws.com/#{S3_BUCKET}/#{path}?AWSAccessKeyId=#{S3_ACCESS_KEY_ID}&Expires=#{expire_date}&Signature=#{signature}"
end
end
class String
def encode_signs
signs = {'+' => "%2B", '=' => "%3D", '?' => '%3F', '@' => '%40',
'$' => '%24', '&' => '%26', ',' => '%2C', '/' => '%2F', ':' => '%3A',
';' => '%3B', '?' => '%3F'}
signs.keys.each do |key|
self.gsub!(key, signs[key])
end
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment