Class: WAZ::Blobs::Service

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

Overview

This is internally used by the waz-blobs part of the gem and it exposes the Windows Azure Blob API REST methods implementation. You can use this class to perform an specific operation that isn’t provided by the current API.

Instance Attribute Summary

Attributes included from Storage::SharedKeyCoreService

#access_key, #account_name, #base_url, #retry_count, #sharedaccesssignature, #type_of_service, #use_devenv, #use_sas_auth_only, #use_ssl

Instance Method Summary collapse

Methods included from Storage::SharedKeyCoreService

#canonicalize_headers, #canonicalize_message, #canonicalize_message20090919, #execute, #generate_request, #generate_request_uri, #generate_signature, #generate_signature20090919, #initialize

Instance Method Details

#copy_blob(source_path, dest_path) ⇒ Object

Copies a blob within the same account (not necessarily to the same container)



152
153
154
# File 'lib/waz/blobs/service.rb', line 152

def copy_blob(source_path, dest_path)
  execute :put, dest_path, nil, { :x_ms_version => "2011-08-18", :x_ms_copy_source => canonicalize_message(source_path) }
end

#create_container(container_name) ⇒ Object

Creates a container on the current Windows Azure Storage account.



9
10
11
# File 'lib/waz/blobs/service.rb', line 9

def create_container(container_name)
  execute :put, container_name, {:restype => 'container'}, {:x_ms_version => '2011-08-18'}
end

#delete_blob(path) ⇒ Object

Deletes the blob existing on the current path.



132
133
134
# File 'lib/waz/blobs/service.rb', line 132

def delete_blob(path)
  execute :delete, path, nil, {:x_ms_version => "2011-08-18"}
end

#delete_container(container_name) ⇒ Object

Deletes the given container from the Windows Azure Storage account.



59
60
61
# File 'lib/waz/blobs/service.rb', line 59

def delete_container(container_name)
  execute :delete, container_name, {:restype => 'container'}, {:x_ms_version => '2011-08-18'}
end

#get_blob(path, options = {}) ⇒ Object

Retrieves a blob (content + headers) from the current path.



127
128
129
# File 'lib/waz/blobs/service.rb', line 127

def get_blob(path, options = {})
  execute :get, path, options, {:x_ms_version => "2011-08-18"}
end

#get_blob_properties(path, options = {}) ⇒ Object

Retrieves the properties associated with the blob at the given path.



137
138
139
# File 'lib/waz/blobs/service.rb', line 137

def get_blob_properties(path, options = {})
  execute(:head, path, options, {:x_ms_version => "2011-08-18"}).headers
end

#get_container_acl(container_name) ⇒ Object

Retrieves the value of the :x_ms_prop_publicaccess header from the container properties indicating whether the container is publicly accessible or not.



29
30
31
32
# File 'lib/waz/blobs/service.rb', line 29

def get_container_acl(container_name)
  headers = execute(:get, container_name, { :restype => 'container', :comp => 'acl' }, {:x_ms_version => '2011-08-18'}).headers
  headers[:x_ms_blob_public_access]
end

#get_container_properties(container_name) ⇒ Object

Retrieves all the properties existing on the container.



14
15
16
# File 'lib/waz/blobs/service.rb', line 14

def get_container_properties(container_name)
  execute(:get, container_name, {:restype => 'container'}, {:x_ms_version => '2011-08-18'}).headers
end

#list_blobs(container_name) ⇒ Object

Lists all the blobs inside the given container.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/waz/blobs/service.rb', line 64

def list_blobs(container_name)
  content = execute(:get, container_name, { :restype => 'container', :comp => 'list'}, {:x_ms_version => '2011-08-18'})
  doc = REXML::Document.new(content)
  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.elements["Properties"], "Content-Type").text }

  end
  return containers
end

#list_blocks(path, block_list_type = 'all') ⇒ Object

Retrieves the list of blocks associated with a single blob. The list is filtered (or not) by type of blob



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/waz/blobs/service.rb', line 162

def list_blocks(path, block_list_type = 'all')
  raise WAZ::Storage::InvalidParameterValue , {:name => :blocklisttype, :values => ['all', 'uncommitted', 'committed']} unless (block_list_type or "") =~ /all|committed|uncommitted/i
  content = execute(:get, path, {:comp => 'blocklist'}.merge(:blocklisttype => block_list_type.downcase), { :x_ms_version => "2009-04-14" })
  doc = REXML::Document.new(content)
  blocks = []
  REXML::XPath.each(doc, '//Block/') do |item|
    blocks << { :name => REXML::XPath.first(item, "Name").text,
                :size => REXML::XPath.first(item, "Size").text,
                :committed => item.parent.name == "CommittedBlocks" }
  end
  return blocks
end

#list_containers(options = {}) ⇒ Object

Lists all the containers existing on the current storage account.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/waz/blobs/service.rb', line 46

def list_containers(options = {})
  content = execute(:get, nil, options.merge(:comp => 'list'))
  doc = REXML::Document.new(content)
  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

Stores a blob on the given container.

Remarks path and payload are just text.

content_type is required by the blobs api, but on this method is defaulted to “application/octect-stream”

metadata is a hash that stores all the properties that you want to add to the blob when creating it.



109
110
111
112
# File 'lib/waz/blobs/service.rb', line 109

def put_blob(path, payload, content_type = "application/octet-stream",  = {})
  default_headers = {"Content-Type" => content_type, :x_ms_version => "2011-08-18", :x_ms_blob_type => "BlockBlob", :x_ms_meta_railsetag => Digest::MD5.hexdigest(payload)}
  execute :put, path, nil, .merge(default_headers), payload
end

#put_block(path, identifier, payload) ⇒ Object

Adds a block to the block list of the given blob



157
158
159
# File 'lib/waz/blobs/service.rb', line 157

def put_block(path, identifier, payload)
  execute :put, path, { :comp => 'block', :blockid => identifier }, {'Content-Type' => "application/octet-stream"}, payload
end

#put_block_list(path, blockids, content_type = "application/octet-stream", metadata = {}) ⇒ Object

Commits a list of blocks to the given blob.

blockids is a list of valid, already-uploaded block IDs (base64-encoded)

content_type is required by the blobs api, but on this method is defaulted to “application/octect-stream”

metadata is a hash that stores all the properties that you want to add to the blob when creating it.



121
122
123
124
# File 'lib/waz/blobs/service.rb', line 121

def put_block_list(path, blockids, content_type = "application/octet-stream",  = {})
  default_headers = {"Content-Type" => "application/xml", "x-ms-blob-content-type" => content_type, :x_ms_version => "2011-08-18"}
  execute :put, path, { :comp => 'blocklist' }, .merge(default_headers), '<?xml version="1.0" encoding="utf-8"?><BlockList>' + blockids.map {|id| "<Latest>#{id.rstrip}</Latest>"}.join + '</BlockList>'
end

#set_blob_metadata(path, metadata = {}) ⇒ Object

Set user defined metadata - overwrites any previous metadata key:value pairs



147
148
149
# File 'lib/waz/blobs/service.rb', line 147

def (path,  = {}) 
  execute :put, path, { :comp => 'metadata' }, .merge({:x_ms_version => "2011-08-18"})
end

#set_blob_properties(path, properties = {}) ⇒ Object

Sets the properties (metadata) associated to the blob at given path.



142
143
144
# File 'lib/waz/blobs/service.rb', line 142

def set_blob_properties(path, properties ={})
  execute :put, path, { :comp => 'properties' }, properties.merge({:x_ms_version => "2011-08-18"})
end

#set_container_acl(container_name, public_available = WAZ::Blobs::BlobSecurity::Private) ⇒ Object

Sets the value of the :x_ms_prop_publicaccess header from the container properties indicating whether the container is publicly accessible or not.

Default is false



39
40
41
42
43
# File 'lib/waz/blobs/service.rb', line 39

def set_container_acl(container_name, public_available = WAZ::Blobs::BlobSecurity::Private)
  publicity = {:x_ms_version => '2011-08-18' }
  publicity[:x_ms_blob_public_access] = public_available unless public_available == WAZ::Blobs::BlobSecurity::Private
  execute :put, container_name, { :restype => 'container', :comp => 'acl' }, publicity
end

#set_container_properties(container_name, properties = {}) ⇒ Object

Set the container properties (metadata).

Remember that custom properties should be named as :x_ms_meta_propertyName in order to have Windows Azure to persist them.



22
23
24
# File 'lib/waz/blobs/service.rb', line 22

def set_container_properties(container_name, properties = {})
  execute :put, container_name, { :restype => 'container', :comp => 'metadata' }, properties.merge!({:x_ms_version => '2011-08-18'})
end

#snapshot_blob(path) ⇒ Object

Creates a read-only snapshot of a blob as it looked like in time.



176
177
178
# File 'lib/waz/blobs/service.rb', line 176

def snapshot_blob(path)
  execute(:put, path, { :comp => 'snapshot' }, {:x_ms_version => "2011-08-18"}).headers[:x_ms_snapshot]
end

#statistics(container_name, add_options = {}) ⇒ Hash

Returns statistics of the given container.

Parameters:

  • container_name (String)
  • add_options (Hash) (defaults to: {})

Options Hash (add_options):

  • :maxresults (String)

    max blobs(5,000 at most)

  • :marker (String)

    marker of a page(“2!80!MDAwMDE0***********–”)

Returns:

  • (Hash)

    => Integer, :files => Integer, :marker => String



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/waz/blobs/service.rb', line 85

def statistics(container_name, add_options={})
  options = { :restype => 'container', :comp => 'list'}
  options.merge!(add_options)

  content = execute(:get, container_name, options, {:x_ms_version => '2011-08-18'})
  doc = REXML::Document.new(content)
  size = 0
  files = 0
  REXML::XPath.each(doc, '//Blob/') do |item|
    size = size + REXML::XPath.first(item.elements["Properties"], "Content-Length").text.to_i
    files = files + 1
  end

  next_marker = REXML::XPath.first(doc, '//NextMarker')
  {:size => size, :files => files, :next_marker => next_marker.text}
end