Class: WAZ::Blobs::Service
- Inherits:
-
Object
- Object
- WAZ::Blobs::Service
- Defined in:
- lib/waz/blobs/service.rb
Instance Attribute Summary collapse
-
#account_key ⇒ Object
Returns the value of attribute account_key.
-
#account_name ⇒ Object
Returns the value of attribute account_name.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#use_ssl ⇒ Object
Returns the value of attribute use_ssl.
Class Method Summary collapse
Instance Method Summary collapse
- #canonicalize_message(url) ⇒ Object
- #create_container(container_name) ⇒ Object
- #delete_blob(path) ⇒ Object
- #delete_container(container_name) ⇒ Object
- #generate_request(verb, url, headers = {}, payload = nil) ⇒ Object
- #generate_request_uri(operation = nil, path = nil, options = {}) ⇒ Object
- #generate_signature(request) ⇒ Object
- #get_blob(path) ⇒ Object
- #get_blob_properties(path) ⇒ Object
- #get_container_acl(container_name) ⇒ Object
- #get_container_properties(container_name) ⇒ Object
-
#initialize(account_name, account_key, use_ssl = false, base_url = "blob.core.windows.net") ⇒ Service
constructor
A new instance of Service.
- #list_blobs(container_name) ⇒ Object
- #list_containers(options = {}) ⇒ Object
- #put_blob(path, payload, content_type = "application/octet-stream", metadata = {}) ⇒ Object
- #set_blob_properties(path, properties = {}) ⇒ Object
- #set_container_acl(container_name, public_available = false) ⇒ Object
- #set_container_properties(container_name, properties = {}) ⇒ Object
Constructor Details
#initialize(account_name, account_key, use_ssl = false, base_url = "blob.core.windows.net") ⇒ Service
Returns a new instance of Service.
6 7 8 9 10 11 |
# File 'lib/waz/blobs/service.rb', line 6 def initialize(account_name, account_key, use_ssl = false, base_url = "blob.core.windows.net" ) self.account_name = account_name self.account_key = account_key self.use_ssl = use_ssl self.base_url = base_url end |
Instance Attribute Details
#account_key ⇒ Object
Returns the value of attribute account_key.
4 5 6 |
# File 'lib/waz/blobs/service.rb', line 4 def account_key @account_key end |
#account_name ⇒ Object
Returns the value of attribute account_name.
4 5 6 |
# File 'lib/waz/blobs/service.rb', line 4 def account_name @account_name end |
#base_url ⇒ Object
Returns the value of attribute base_url.
4 5 6 |
# File 'lib/waz/blobs/service.rb', line 4 def base_url @base_url end |
#use_ssl ⇒ Object
Returns the value of attribute use_ssl.
4 5 6 |
# File 'lib/waz/blobs/service.rb', line 4 def use_ssl @use_ssl end |
Class Method Details
.canonicalize_headers(headers) ⇒ Object
123 124 125 126 |
# File 'lib/waz/blobs/service.rb', line 123 def self.canonicalize_headers(headers) cannonicalized_headers = headers.keys.select {|h| h.to_s.start_with? 'x-ms'}.map{ |h| "#{h.downcase.strip}:#{headers[h].strip}" }.sort{ |a, b| a <=> b }.join("\x0A") return cannonicalized_headers end |
Instance Method Details
#canonicalize_message(url) ⇒ Object
128 129 130 131 |
# File 'lib/waz/blobs/service.rb', line 128 def (url) uri_component = url.gsub(/https?:\/\/[^\/]+\//i, '').scan(/([^&]+)/i).first() = "/#{self.account_name}/#{uri_component}" end |
#create_container(container_name) ⇒ Object
13 14 15 16 17 |
# File 'lib/waz/blobs/service.rb', line 13 def create_container(container_name) url = generate_request_uri(nil, container_name) request = generate_request("PUT", url) request.execute() end |
#delete_blob(path) ⇒ Object
87 88 89 90 91 |
# File 'lib/waz/blobs/service.rb', line 87 def delete_blob(path) url = generate_request_uri(nil, path) request = generate_request("DELETE", url) request.execute() end |
#delete_container(container_name) ⇒ Object
56 57 58 59 60 |
# File 'lib/waz/blobs/service.rb', line 56 def delete_container(container_name) url = generate_request_uri(nil, container_name) request = generate_request("DELETE", url) request.execute() end |
#generate_request(verb, url, headers = {}, payload = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/waz/blobs/service.rb', line 105 def generate_request(verb, url, headers = {}, payload = nil) http_headers = {} headers.each{ |k, v| http_headers[k.to_s.gsub(/_/, '-')] = v} request = RestClient::Request.new(:method => verb.downcase.to_sym, :url => url, :headers => http_headers, :payload => payload) request.headers["x-ms-Date"] = Time.new.httpdate request.headers["Content-Length"] = (request.payload or "").length request.headers["Authorization"] = "SharedKey #{account_name}:#{generate_signature(request)}" return request end |
#generate_request_uri(operation = nil, path = nil, options = {}) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/waz/blobs/service.rb', line 115 def generate_request_uri(operation = nil, path = nil, = {}) protocol = use_ssl ? "https" : "http" query_params = .keys.sort{ |a, b| a.to_s <=> b.to_s}.map{ |k| "#{k.to_s.gsub(/_/, '')}=#{[k]}"}.join("&") unless .empty? uri = "#{protocol}://#{account_name}.#{base_url}#{(path or "").start_with?("/") ? "" : "/"}#{(path or "")}#{operation ? "?comp=" + operation : ""}" uri << "#{operation ? "&" : "?"}#{query_params}" if query_params return uri end |
#generate_signature(request) ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/waz/blobs/service.rb', line 133 def generate_signature(request) signature = request.method.to_s.upcase + "\x0A" + (request.headers["Content-MD5"] or "") + "\x0A" + (request.headers["Content-Type"] or "") + "\x0A" + (request.headers["Date"] or "")+ "\x0A" + self.class.canonicalize_headers(request.headers) + "\x0A" + (request.url) return Base64.encode64(HMAC::SHA256.new(Base64.decode64(self.account_key)).update(signature.toutf8).digest) end |
#get_blob(path) ⇒ Object
81 82 83 84 85 |
# File 'lib/waz/blobs/service.rb', line 81 def get_blob(path) url = generate_request_uri(nil, path) request = generate_request("GET", url) request.execute() end |
#get_blob_properties(path) ⇒ Object
93 94 95 96 97 |
# File 'lib/waz/blobs/service.rb', line 93 def get_blob_properties(path) url = generate_request_uri(nil, path) request = generate_request("HEAD", url) request.execute().headers end |
#get_container_acl(container_name) ⇒ Object
31 32 33 34 35 |
# File 'lib/waz/blobs/service.rb', line 31 def get_container_acl(container_name) url = generate_request_uri("acl", container_name) request = generate_request("GET", url) request.execute().headers[:x_ms_prop_publicaccess].downcase == true.to_s end |
#get_container_properties(container_name) ⇒ Object
19 20 21 22 23 |
# File 'lib/waz/blobs/service.rb', line 19 def get_container_properties(container_name) url = generate_request_uri(nil, container_name) request = generate_request("GET", url) request.execute().headers end |
#list_blobs(container_name) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/waz/blobs/service.rb', line 62 def list_blobs(container_name) url = generate_request_uri("list", container_name) request = generate_request("GET", url) doc = REXML::Document.new(request.execute()) containers = [] REXML::XPath.each(doc, '//Blob/') do |item| containers << { :name => REXML::XPath.first(item, "Name").text, :url => REXML::XPath.first(item, "Url").text, :content_type => REXML::XPath.first(item, "ContentType").text } end return containers end |
#list_containers(options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/waz/blobs/service.rb', line 43 def list_containers( = {}) url = generate_request_uri("list", nil, ) request = generate_request("GET", url) doc = REXML::Document.new(request.execute()) containers = [] REXML::XPath.each(doc, '//Container/') do |item| containers << { :name => REXML::XPath.first(item, "Name").text, :url => REXML::XPath.first(item, "Url").text, :last_modified => REXML::XPath.first(item, "LastModified").text} end return containers end |
#put_blob(path, payload, content_type = "application/octet-stream", metadata = {}) ⇒ Object
75 76 77 78 79 |
# File 'lib/waz/blobs/service.rb', line 75 def put_blob(path, payload, content_type = "application/octet-stream", = {}) url = generate_request_uri(nil, path) request = generate_request("PUT", url, .merge("Content-Type" => content_type), payload) request.execute() end |
#set_blob_properties(path, properties = {}) ⇒ Object
99 100 101 102 103 |
# File 'lib/waz/blobs/service.rb', line 99 def set_blob_properties(path, properties ={}) url = generate_request_uri("metadata", path) request = generate_request("PUT", url, properties) request.execute() end |
#set_container_acl(container_name, public_available = false) ⇒ Object
37 38 39 40 41 |
# File 'lib/waz/blobs/service.rb', line 37 def set_container_acl(container_name, public_available = false) url = generate_request_uri("acl", container_name) request = generate_request("PUT", url, "x-ms-prop-publicaccess" => public_available.to_s) request.execute() end |
#set_container_properties(container_name, properties = {}) ⇒ Object
25 26 27 28 29 |
# File 'lib/waz/blobs/service.rb', line 25 def set_container_properties(container_name, properties = {}) url = generate_request_uri("metadata", container_name) request = generate_request("PUT", url, properties) request.execute() end |