Created
February 20, 2014 13:34
-
-
Save mbajur/9113672 to your computer and use it in GitHub Desktop.
AZURE SAS URL Generator
This file contains hidden or 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
require 'time' | |
require 'openssl' | |
require "base64" | |
require 'uri' | |
require 'addressable/uri' | |
def create_signature(path = '/', resource = 'b', permissions = 'r', start = '', expiry = '', identifier = '') | |
# If resource is a container, remove the last part (which is the filename) | |
path = path.split('/').reverse.drop(1).reverse.join('/') if resource == 'c' | |
canonicalizedResource = "/mediasvc78m7lfh2gnn2x/#{path}" | |
stringToSign = [] | |
stringToSign << permissions | |
stringToSign << start | |
stringToSign << expiry | |
stringToSign << canonicalizedResource | |
stringToSign << identifier | |
stringToSign = stringToSign.join("\n") | |
signature = OpenSSL::HMAC.digest('sha256', wms_api_key, stringToSign.encode(Encoding::UTF_8)) | |
signature = Base64.encode64(signature) | |
return signature | |
end | |
def createSignedQueryString(path = '/', query_string = '', resource = 'b', permissions = 'r', start = '', expiry = '', identifier = '') | |
base = 'https://mediasvc78m7lfh2gnn2x.blob.core.windows.net' | |
uri = Addressable::URI.new | |
# Parts | |
parts = {} | |
parts[:st] = URI.unescape(start) unless start == '' | |
parts[:se] = URI.unescape(expiry) | |
parts[:sr] = URI.unescape(resource) | |
parts[:sp] = URI.unescape(permissions) | |
parts[:si] = URI.unescape(identifier) unless identifier == '' | |
parts[:sig] = URI.unescape( create_signature(path, 'b', 'r', start, expiry) ) | |
uri.query_values = parts | |
return "#{base}/#{path}?#{uri.query}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment