Class WAZ::Blobs::Container
In: lib/waz/blobs/container.rb
Parent: Object

This class is used to model the Container inside Windows Azure Blobs the usage it‘s pretty simple, here you can see a couple of samples. Here you can find Microsoft‘s REST API description available on MSDN at

 # list available containers

 # create a container

 # get a specific container
 my_container = WAZ::Blobs::Container.find('my-container')

 # get container properties (including default headers)
 my_container.metadata #=> hash containing beautified metadata (:x_ms_meta_name)

 # set container properties (should follow x-ms-meta to be persisted)
 my_container.put_properties(:x_ms_meta_MyProperty => "my value")

 # get a the value indicating whether the container is public or not
 my_container.public_access? #=> true or false based on x-ms-prop-publicaccess

 # set a value indicating whether the container is public or not
 my_container.public_access = false

 # delete container

 # store a blob on the given container'my-blob', blob_content, 'application/xml')

 # retrieve a particular blob from a container

 # retrieve a blob list from a container
 my_container.blobs #=> WAZ::Blobs::BlobObject collection



name  [RW] 

Public Class methods

Creates a new container with the given name.


    # File lib/waz/blobs/container.rb, line 43
43:         def create(name)
44:           raise WAZ::Storage::InvalidParameterValue, {:name => "name", :values => ["lower letters, numbers or - (hypen), and must not start or end with - (hyphen)"]} unless WAZ::Storage::ValidationRules.valid_name?(name)
45:           service_instance.create_container(name)
46:           return => name)
47:         end

Finds a container by name. It will return nil if no container was found.


    # File lib/waz/blobs/container.rb, line 50
50:         def find(name)
51:           begin 
52:             properties = service_instance.get_container_properties(name)
53:             return => name))
54:           rescue RestClient::ResourceNotFound
55:             return nil
56:           end
57:         end

Returns all the containers on the given account.


    # File lib/waz/blobs/container.rb, line 60
60:         def list(options = {})
61:           service_instance.list_containers(options).map { |container| }
62:         end

Creates a new instance of the WAZ::Blobs::Container. This class isn‘t intended for external use to access or create a container you should use the class methods provided like list, create, or find.


    # File lib/waz/blobs/container.rb, line 77
77:       def initialize(options = {})
78:         raise WAZ::Storage::InvalidOption, :name unless options.keys.include?(:name) and !options[:name].empty?
79: = options[:name]
80:       end

This method is internally used by this class. It‘s the way we keep a single instance of the service that wraps the calls the Windows Azure Blobs API. It‘s initialized with the values from the default_connection on WAZ::Storage::Base initialized thru establish_connection!


    # File lib/waz/blobs/container.rb, line 67
67:         def service_instance
68:           options = WAZ::Storage::Base.default_connection.merge(:type_of_service => "blob")
69:           (@service_instances ||= {})[options[:account_name]] ||=
70:         end

Public Instance methods

Retrieves the blob by the given name. If the blob isn‘t found on the current container it will return nil instead of throwing an exception.


     # File lib/waz/blobs/container.rb, line 142
142:       def [](blob_name)
143:         begin
144:           blob_name.gsub!(%r{^/}, '')
145:           properties = self.class.service_instance.get_blob_properties("#{}/#{blob_name}")
146:           return => blob_name, 
147:                                 :url => self.class.service_instance.generate_request_uri("#{}/#{blob_name}"),
148:                                 :content_type => properties[:content_type])
149:         rescue RestClient::ResourceNotFound
150:           return nil
151:         end
152:       end

Returns a list of blobs (WAZ::Blobs::BlobObject) contained on the current container.


     # File lib/waz/blobs/container.rb, line 109
109:       def blobs
110:         self.class.service_instance.list_blobs(name).map { |blob| }
111:       end

Removes the container from the current account.


    # File lib/waz/blobs/container.rb, line 94
94:       def destroy!
95:         self.class.service_instance.delete_container(
96:       end

Returns the container metadata.


    # File lib/waz/blobs/container.rb, line 83
83:       def metadata
84:         self.class.service_instance.get_container_properties(
85:       end

Sets a value indicating whether the container is public accessible (i.e. from a Web Browser) or not.


     # File lib/waz/blobs/container.rb, line 104
104:       def public_access=(value)
105:         self.class.service_instance.set_container_acl(, value)
106:       end

Retuns a value indicating whether the container is public accessible (i.e. from a Web Browser) or not.


     # File lib/waz/blobs/container.rb, line 99
 99:       def public_access?
100:         self.class.service_instance.get_container_acl(
101:       end

Adds metadata for the container.Those properties are sent as HTTP Headers it‘s really important that you name your custom properties with the x-ms-meta prefix, if not the won‘t be persisted by the Windows Azure Blob Storage API.


    # File lib/waz/blobs/container.rb, line 89
89:       def put_properties!(properties = {})
90:         self.class.service_instance.set_container_properties(, properties)
91:       end

Stores a blob on the container with under the given name, with the given content and the required content_type. <strong>The options parameters if provided will set the default metadata properties for the blob</strong>.


     # File lib/waz/blobs/container.rb, line 116
116:       def store(blob_name, payload, content_type, options = {})
117:         blob_name.gsub!(%r{^/}, '')
118:         self.class.service_instance.put_blob("#{}/#{blob_name}", payload, content_type, options)
119:         return => blob_name, 
120:                               :url => self.class.service_instance.generate_request_uri("#{}/#{blob_name}"),
121:                               :content_type => content_type)
122:       end

Uploads the contents of a stream to the specified blob within this container, using the required content_type. The stream will be uploaded in blocks of size block_size bytes, which defaults to four megabytes. <strong>The options parameter, if provided, will set the default metadata properties for the blob</strong>.


     # File lib/waz/blobs/container.rb, line 128
128:       def upload(blob_name, stream, content_type, options = {}, block_size = 4 * 2**20)
129:         blob_name.gsub!(%r{^/}, '')
130:         path = "#{}/#{blob_name}"
131:         n = 0
132:         until stream.eof?
133:           self.class.service_instance.put_block path, Base64.encode64('%064d' % n),
134:           n += 1
135:         end
136:         self.class.service_instance.put_block_list path, (0...n).map{|id| Base64.encode64('%064d' % id)}, content_type, options
137:         return => blob_name, :url => self.class.service_instance.generate_request_uri("#{}/#{blob_name}"), :content_type => content_type)
138:       end