Skip to content

Instantly share code, notes, and snippets.

@ayosec
Created April 29, 2015 01:10
Show Gist options
  • Save ayosec/d8fb1dc03d85dbc01adf to your computer and use it in GitHub Desktop.
Save ayosec/d8fb1dc03d85dbc01adf to your computer and use it in GitHub Desktop.
Presigned URL to download objects from S3
require "openssl"
require "base64"
access_key = ".........."
secret_key = "..................."
s3_object = "$BUCKET/$PATH"
expires = (Time.now + 300).to_i # 5 minutes
data = %[GET\n\n\n#{expires}\n/#{s3_object}]
digest = OpenSSL::Digest.new('sha1')
hmac = OpenSSL::HMAC.digest(digest, secret_key, data)
signature = Base64.urlsafe_encode64(hmac)
puts "https://s3.amazonaws.com/#{s3_object}?AWSAccessKeyId=#{access_key}&Signature=#{signature}&Expires=#{expires}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment