Class: WAZ::Blobs::BlobObject
- Inherits:
-
Object
- Object
- WAZ::Blobs::BlobObject
- 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
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#name ⇒ Object
Returns the value of attribute name.
-
#snapshot_date ⇒ Object
Returns the value of attribute snapshot_date.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
-
.service_instance ⇒ Object
This method is internally used by this class.
Instance Method Summary collapse
-
#copy(destination) ⇒ Object
Copies the blob to the destination and returns the copied blob instance.
-
#destroy! ⇒ Object
Removes the blob from the container.
-
#initialize(options = {}) ⇒ BlobObject
constructor
Creates a new instance of the Blob object.
-
#metadata ⇒ Object
Returns the blob properties from Windows Azure.
-
#path ⇒ Object
Returns the blob path.
-
#put_metadata!(metadata = {}) ⇒ Object
Stores blob metadata.
-
#put_properties!(properties = {}) ⇒ Object
Stores the blob properties.
-
#snapshot ⇒ Object
Creates and returns a read-only snapshot of a blob as it looked like in time.
-
#value ⇒ Object
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.
-
#value=(new_value) ⇒ Object
Assigns the given value to the blob content.
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 |
# File 'lib/waz/blobs/blob_object.rb', line 44 def initialize( = {}) raise WAZ::Storage::InvalidOption, :name unless .keys.include?(:name) and ![:name].empty? raise WAZ::Storage::InvalidOption, :url unless .keys.include?(:url) and ![:url].empty? raise WAZ::Storage::InvalidOption, :content_type unless .keys.include?(:content_type) and ![:content_type].empty? self.name = [:name] self.url = [:url] self.content_type = [:content_type] self.snapshot_date = [:snapshot_date] end |
Instance Attribute Details
#content_type ⇒ Object
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 |
#name ⇒ Object
Returns the value of attribute name.
38 39 40 |
# File 'lib/waz/blobs/blob_object.rb', line 38 def name @name end |
#snapshot_date ⇒ Object
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 |
#url ⇒ Object
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_instance ⇒ Object
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 = WAZ::Storage::Base.default_connection.merge(:type_of_service => "blob") (@service_instances ||= {})[[:account_name]] ||= Service.new() 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”
96 97 98 99 100 101 102 |
# File 'lib/waz/blobs/blob_object.rb', line 96 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.
88 89 90 |
# File 'lib/waz/blobs/blob_object.rb', line 88 def destroy! self.class.service_instance.delete_blob(path) end |
#metadata ⇒ Object
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.
56 57 58 |
# File 'lib/waz/blobs/blob_object.rb', line 56 def self.class.service_instance.get_blob_properties(path) end |
#path ⇒ Object
Returns the blob path. This is specially important when simulating containers inside containers by enabling the API to point to the appropiated resource.
116 117 118 |
# File 'lib/waz/blobs/blob_object.rb', line 116 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’.
83 84 85 |
# File 'lib/waz/blobs/blob_object.rb', line 83 def ( = {}) 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.
76 77 78 79 |
# File 'lib/waz/blobs/blob_object.rb', line 76 def put_properties!(properties = {}) raise WAZ::Blobs::InvalidOperation if self.snapshot_date self.class.service_instance.set_blob_properties(path, properties) end |
#snapshot ⇒ Object
Creates and returns a read-only snapshot of a blob as it looked like in time.
105 106 107 108 109 110 111 112 |
# File 'lib/waz/blobs/blob_object.rb', line 105 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 |
#value ⇒ Object
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.
62 63 64 |
# File 'lib/waz/blobs/blob_object.rb', line 62 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.
68 69 70 71 72 |
# File 'lib/waz/blobs/blob_object.rb', line 68 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 |