Class: WAZ::Blobs::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/waz/blobs/service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(, , use_ssl = false, base_url = "blob.core.windows.net" )
  self. = 
  self. =  
  self.use_ssl = use_ssl
  self.base_url = base_url
end

Instance Attribute Details

#account_keyObject

Returns the value of attribute account_key.



4
5
6
# File 'lib/waz/blobs/service.rb', line 4

def 
  @account_key
end

#account_nameObject

Returns the value of attribute account_name.



4
5
6
# File 'lib/waz/blobs/service.rb', line 4

def 
  @account_name
end

#base_urlObject

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_sslObject

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 canonicalize_message(url)
  uri_component = url.gsub(/https?:\/\/[^\/]+\//i, '').scan(/([^&]+)/i).first()
  cannonicalized_message = "/#{self.}/#{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 #{}:#{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, options = {})
  protocol = use_ssl ? "https" : "http"
  query_params = options.keys.sort{ |a, b| a.to_s <=> b.to_s}.map{ |k| "#{k.to_s.gsub(/_/, '')}=#{options[k]}"}.join("&") unless options.empty?
  uri = "#{protocol}://#{}.#{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" +
              canonicalize_message(request.url)
  return Base64.encode64(HMAC::SHA256.new(Base64.decode64(self.)).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(options = {})
  url = generate_request_uri("list", nil, options)
  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