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, #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)



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

def copy_blob(source_path, dest_path)
  execute :put, dest_path, nil, { :x_ms_version => "2009-09-19", :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 => '2009-09-19'}
end

#delete_blob(path) ⇒ Object

Deletes the blob existing on the current path.



107
108
109
# File 'lib/waz/blobs/service.rb', line 107

def delete_blob(path)
  execute :delete, path, nil, {:x_ms_version => "2009-09-19"}
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 => '2009-09-19'}
end

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

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



102
103
104
# File 'lib/waz/blobs/service.rb', line 102

def get_blob(path, options = {})
  execute :get, path, options, {:x_ms_version => "2009-09-19"}
end

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

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



112
113
114
# File 'lib/waz/blobs/service.rb', line 112

def get_blob_properties(path, options = {})
  execute(:head, path, options, {:x_ms_version => "2009-09-19"}).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 => '2009-09-19'}).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 => '2009-09-19'}).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 => '2009-09-19'})
  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



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/waz/blobs/service.rb', line 137

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.



84
85
86
87
# File 'lib/waz/blobs/service.rb', line 84

def put_blob(path, payload, content_type = "application/octet-stream",  = {})
  default_headers = {"Content-Type" => content_type, :x_ms_version => "2009-09-19", :x_ms_blob_type => "BlockBlob"}
  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



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

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.



96
97
98
99
# File 'lib/waz/blobs/service.rb', line 96

def put_block_list(path, blockids, content_type = "application/octet-stream",  = {})
  default_headers = {"Content-Type" => content_type, :x_ms_version => "2009-09-19"}
  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



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

def (path,  = {}) 
  execute :put, path, { :comp => 'metadata' }, .merge({:x_ms_version => "2009-09-19"})
end

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

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



117
118
119
# File 'lib/waz/blobs/service.rb', line 117

def set_blob_properties(path, properties ={})
  execute :put, path, { :comp => 'properties' }, properties.merge({:x_ms_version => "2009-09-19"})
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 => '2009-09-19' }
  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 => '2009-09-19'})
end

#snapshot_blob(path) ⇒ Object

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



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

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