Created
October 21, 2015 02:21
-
-
Save oestrich/44602a5262ad3c469681 to your computer and use it in GitHub Desktop.
Signing Google Cloud Storage URLs
This file contains 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
private_key = "..." | |
client_email = "..." | |
bucket = "..." | |
path = "..." | |
full_path = "/#{bucket}/#{path}" | |
expiration = 5.minutes.from_now.to_i | |
signature_string = [ | |
"GET", | |
"", | |
"", | |
expiration, | |
full_path, | |
].join("\n") | |
digest = OpenSSL::Digest::SHA256.new | |
signer = OpenSSL::PKey::RSA.new(private_key) | |
signature = Base64.strict_encode64(signer.sign(digest, signature_string)) | |
signature = CGI.escape(signature) | |
"https://storage.googleapis.com#{full_path}?GoogleAccessId=#{client_email}&Expires=#{expiration}&Signature=#{signature}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A suggestion for rubocop builds, and read the key from the google json directly
again, thanks man, it help me a lot!