Class WAZ::Blobs::BlobObject
In: lib/waz/blobs/blob_object.rb
Parent: Object

This class is used to model the Blob inside Windows Azure Blobs the usage it‘s pretty simple, here you can see a couple of samples. These are the implemented methods of the blob API up to now. The basics are implemented although blocks management is not yet completed, I consider that the API is pretty usable since it covers the basics

  # retrieve blob name, uri and content-type
  blob.name
  blob.url
  blob.content_type

  # retrieve blob value
  blob.value #=> lazy loaded payload of the blob

  # retrieve blob metadata (+ properties)
  blob.metadata #=> hash containing beautified metadata (:x_ms_meta_name)

  # put blob metadata
  blob.put_properties(:x_ms_meta_MyProperty => "my value")

  # update value
  blob.value = "my new value" #=> this will update the blob content on WAZ

REMARKS: This class is not meant to be manually instanciated it‘s basicaly an internal representation returned by the WAZ::Blobs::Container.

Methods

Attributes

content_type  [RW] 
name  [RW] 
snapshot_date  [RW] 
url  [RW] 

Public Class methods

Creates a new instance of the Blob object. This constructor is internally used by the Container it‘s initialized thru a hash that‘s received as parameter. It has the following requirements: :name which is the blob name (usually the file name), :url that is the url of the blob (used to download or access it via browser) and :content_type which is the content type of the blob and is a required parameter by the Azure API

[Source]

    # File lib/waz/blobs/blob_object.rb, line 44
44:       def initialize(options = {})
45:         raise WAZ::Storage::InvalidOption, :name unless options.keys.include?(:name) and !options[:name].empty?
46:         raise WAZ::Storage::InvalidOption, :url unless options.keys.include?(:url) and !options[:url].empty?
47:         raise WAZ::Storage::InvalidOption, :content_type unless options.keys.include?(:content_type) and !options[:content_type].empty?
48:         self.name = options[:name]
49:         self.url = options[:url]
50:         self.content_type = options[:content_type]
51:         self.snapshot_date = options[:snapshot_date]
52:       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/blob_object.rb, line 32
32:         def service_instance
33:           options = WAZ::Storage::Base.default_connection.merge(:type_of_service => "blob")
34:           (@service_instances ||= {})[options[:account_name]] ||= Service.new(options)
35:         end

Public Instance methods

Copies the blob to the destination and returns the copied blob instance.

destination should be formed as "container/blob"

[Source]

     # File lib/waz/blobs/blob_object.rb, line 96
 96:       def copy(destination)
 97:         self.class.service_instance.copy_blob(self.path, destination)
 98:         properties = self.class.service_instance.get_blob_properties(destination)
 99:         return BlobObject.new(:name => destination, 
100:                               :url => self.class.service_instance.generate_request_uri(destination),
101:                               :content_type => properties[:content_type])
102:       end

Removes the blob from the container.

[Source]

    # File lib/waz/blobs/blob_object.rb, line 88
88:       def destroy!
89:         self.class.service_instance.delete_blob(path)
90:       end

Returns the blob properties from Windows Azure. This properties always come as HTTP Headers and they include standard headers like Content-Type, Content-Length, etc. combined with custom properties like with x-ms-meta-Name.

[Source]

    # File lib/waz/blobs/blob_object.rb, line 56
56:       def metadata
57:         self.class.service_instance.get_blob_properties(path)
58:       end

Returns the blob path. This is specially important when simulating containers inside containers by enabling the API to point to the appropiated resource.

[Source]

     # File lib/waz/blobs/blob_object.rb, line 116
116:       def path
117:         url.gsub(/https?:\/\/[^\/]+\//i, '').scan(/([^&]+)/i).first().first()
118:       end

Stores blob metadata. User metadata must be prefixed with ‘x-ms-meta-’. The advantage of this over put_properties is that it only affect user_metadata and doesn‘t overwrite any system values, like ‘content_type’.

[Source]

    # File lib/waz/blobs/blob_object.rb, line 83
83:       def put_metadata!(metadata = {})  
84:         self.class.service_instance.set_blob_metadata(path, metadata)
85:       end

Stores the blob properties. 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/blob_object.rb, line 76
76:       def put_properties!(properties = {})
77:         raise WAZ::Blobs::InvalidOperation if self.snapshot_date
78:         self.class.service_instance.set_blob_properties(path, properties)
79:       end

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

[Source]

     # File lib/waz/blobs/blob_object.rb, line 105
105:       def snapshot
106:         date = self.class.service_instance.snapshot_blob(self.path)
107:         properties = self.class.service_instance.get_blob_properties(self.path)
108:         return BlobObject.new(:name => self.name, 
109:                               :url => self.class.service_instance.generate_request_uri(self.path) + "?snapshot=#{date}",
110:                               :content_type => properties[:content_type],
111:                               :snapshot_date => date)
112:       end

Returns the actual blob content, this method is specially used to avoid retrieving the whole blob while iterating and retrieving the blob collection from the Container.

[Source]

    # File lib/waz/blobs/blob_object.rb, line 62
62:       def value
63:         @value ||= self.class.service_instance.get_blob(path)
64:       end

Assigns the given value to the blob content. It also stores a local copy of it in order to avoid round trips on scenarios when you do Save and Display on the same context.

[Source]

    # File lib/waz/blobs/blob_object.rb, line 68
68:       def value=(new_value)
69:         raise WAZ::Blobs::InvalidOperation if self.snapshot_date
70:         self.class.service_instance.put_blob(path, new_value, content_type, metadata)
71:         @value = new_value
72:       end

[Validate]