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 msdn.microsoft.com/en-us/library/dd179361.aspx

 # list available containers
 WAZ::Blobs::Container.list

 # create a container
 WAZ::Blobs::Container.create('my-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
 my_container.destroy!

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

 # retrieve a particular blob from a container
 my_container['my-blob']

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

Methods

Attributes

name  [RW] 

Public Class methods

Creates a new container with the given name.

[Source]

    # 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 Container.new(:name => name)
47:         end

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

[Source]

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

Returns all the containers on the given account.

[Source]

    # File lib/waz/blobs/container.rb, line 60
60:         def list(options = {})
61:           service_instance.list_containers(options).map { |container| Container.new(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.

[Source]

    # 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:         self.name = 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!

[Source]

    # 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]] ||= Service.new(options)
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.

[Source]

     # 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("#{self.name}/#{blob_name}")
146:           return BlobObject.new(:name => blob_name, 
147:                                 :url => self.class.service_instance.generate_request_uri("#{self.name}/#{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.

[Source]

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

Removes the container from the current account.

[Source]

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

Returns the container metadata.

[Source]

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

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

[Source]

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

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

[Source]

     # File lib/waz/blobs/container.rb, line 99
 99:       def public_access?
100:         self.class.service_instance.get_container_acl(self.name)
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.

[Source]

    # File lib/waz/blobs/container.rb, line 89
89:       def put_properties!(properties = {})
90:         self.class.service_instance.set_container_properties(self.name, 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>.

[Source]

     # 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("#{self.name}/#{blob_name}", payload, content_type, options)
119:         return BlobObject.new(:name => blob_name, 
120:                               :url => self.class.service_instance.generate_request_uri("#{self.name}/#{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>.

[Source]

     # 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 = "#{self.name}/#{blob_name}"
131:         n = 0
132:         until stream.eof?
133:           self.class.service_instance.put_block path, Base64.encode64('%064d' % n), stream.read(block_size)
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 BlobObject.new(:name => blob_name, :url => self.class.service_instance.generate_request_uri("#{self.name}/#{blob_name}"), :content_type => content_type)
138:       end

[Validate]