Class: WAZ::Blobs::BlobObject

Inherits:
Object
  • Object
show all
Defined in:
lib/waz/blobs/blob_object.rb

Overview

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. #=> 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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BlobObject

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



44
45
46
47
48
49
50
51
52
53
# File 'lib/waz/blobs/blob_object.rb', line 44

def initialize(options = {})
  raise WAZ::Storage::InvalidOption, :name unless options.keys.include?(:name) and !options[:name].empty?
  raise WAZ::Storage::InvalidOption, :url unless options.keys.include?(:url) and !options[:url].empty?
  raise WAZ::Storage::InvalidOption, :content_type unless options.keys.include?(:content_type) and !options[:content_type].empty?
  self.name = options[:name]
  self.url = options[:url]
  self.content_type = options[:content_type]
  self.snapshot_date = options[:snapshot_date]
  self.railsetag = options[:railsetag]
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



38
39
40
# File 'lib/waz/blobs/blob_object.rb', line 38

def content_type
  @content_type
end

#nameObject

Returns the value of attribute name.



38
39
40
# File 'lib/waz/blobs/blob_object.rb', line 38

def name
  @name
end

#railsetagObject

Returns the value of attribute railsetag.



38
39
40
# File 'lib/waz/blobs/blob_object.rb', line 38

def railsetag
  @railsetag
end

#snapshot_dateObject

Returns the value of attribute snapshot_date.



38
39
40
# File 'lib/waz/blobs/blob_object.rb', line 38

def snapshot_date
  @snapshot_date
end

#urlObject

Returns the value of attribute url.



38
39
40
# File 'lib/waz/blobs/blob_object.rb', line 38

def url
  @url
end

Class Method Details

.service_instanceObject

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!



32
33
34
35
# File 'lib/waz/blobs/blob_object.rb', line 32

def service_instance
  options = WAZ::Storage::Base.default_connection.merge(:type_of_service => "blob")
  (@service_instances ||= {})[options[:account_name]] ||= Service.new(options)
end

Instance Method Details

#copy(destination) ⇒ Object

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

destination should be formed as “container/blob”



97
98
99
100
101
102
103
# File 'lib/waz/blobs/blob_object.rb', line 97

def copy(destination)
  self.class.service_instance.copy_blob(self.path, destination)
  properties = self.class.service_instance.get_blob_properties(destination)
  return BlobObject.new(:name => destination, 
                        :url => self.class.service_instance.generate_request_uri(destination),
                        :content_type => properties[:content_type])
end

#destroy!Object

Removes the blob from the container.



89
90
91
# File 'lib/waz/blobs/blob_object.rb', line 89

def destroy!
  self.class.service_instance.delete_blob(path)
end

#metadataObject

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.



57
58
59
# File 'lib/waz/blobs/blob_object.rb', line 57

def 
  self.class.service_instance.get_blob_properties(path)
end

#pathObject

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



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

def path
  url.gsub(/https?:\/\/[^\/]+\//i, '').scan(/([^&]+)/i).first().first()
end

#put_metadata!(metadata = {}) ⇒ Object

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’.



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

def put_metadata!( = {})  
  self.class.service_instance.(path, )
end

#put_properties!(properties = {}) ⇒ Object

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.



77
78
79
80
# File 'lib/waz/blobs/blob_object.rb', line 77

def put_properties!(properties = {})
  raise WAZ::Blobs::InvalidOperation if self.snapshot_date
  self.class.service_instance.set_blob_properties(path, properties)
end

#snapshotObject

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



106
107
108
109
110
111
112
113
# File 'lib/waz/blobs/blob_object.rb', line 106

def snapshot
  date = self.class.service_instance.snapshot_blob(self.path)
  properties = self.class.service_instance.get_blob_properties(self.path)
  return BlobObject.new(:name => self.name, 
                        :url => self.class.service_instance.generate_request_uri(self.path) + "?snapshot=#{date}",
                        :content_type => properties[:content_type],
                        :snapshot_date => date)
end

#valueObject

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.



63
64
65
# File 'lib/waz/blobs/blob_object.rb', line 63

def value
  @value ||= self.class.service_instance.get_blob(path)
end

#value=(new_value) ⇒ Object

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.



69
70
71
72
73
# File 'lib/waz/blobs/blob_object.rb', line 69

def value=(new_value)
  raise WAZ::Blobs::InvalidOperation if self.snapshot_date
  self.class.service_instance.put_blob(path, new_value, content_type, )
  @value = new_value
end