Class: WAZ::Blobs::Container

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

Overview

# retrieve a blob list from a container

my_container.blobs #=> WAZ::Blobs::BlobObject collection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Container

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.



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

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



73
74
75
# File 'lib/waz/blobs/container.rb', line 73

def name
  @name
end

Class Method Details

.create(name) ⇒ Object

Creates a new container with the given name.



43
44
45
46
47
# File 'lib/waz/blobs/container.rb', line 43

def create(name)
  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)
  service_instance.create_container(name)
  return Container.new(:name => name)
end

.find(name) ⇒ Object

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



50
51
52
53
54
55
56
57
# File 'lib/waz/blobs/container.rb', line 50

def find(name)
  begin 
    properties = service_instance.get_container_properties(name)
    return Container.new(properties.merge(:name => name))
  rescue RestClient::ResourceNotFound
    return nil
  end
end

.list(options = {}) ⇒ Object

Returns all the containers on the given account.



60
61
62
# File 'lib/waz/blobs/container.rb', line 60

def list(options = {})
  service_instance.list_containers(options).map { |container| Container.new(container) }
end

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



67
68
69
70
# File 'lib/waz/blobs/container.rb', line 67

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

#[](blob_name) ⇒ Object

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.



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/waz/blobs/container.rb', line 126

def [](blob_name)
  begin
    blob_name.gsub!(%r{^/}, '')
    properties = self.class.service_instance.get_blob_properties("#{self.name}/#{blob_name}")
    return BlobObject.new(:name => blob_name, 
                          :url => self.class.service_instance.generate_request_uri("#{self.name}/#{blob_name}"),
                          :content_type => properties[:content_type])
  rescue RestClient::ResourceNotFound
    return nil
  end
end

#blobsObject

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



109
110
111
# File 'lib/waz/blobs/container.rb', line 109

def blobs
  self.class.service_instance.list_blobs(name).map { |blob| WAZ::Blobs::BlobObject.new(blob) }
end

#destroy!Object

Removes the container from the current account.



94
95
96
# File 'lib/waz/blobs/container.rb', line 94

def destroy!
  self.class.service_instance.delete_container(self.name)
end

#metadataObject

Returns the container metadata.



83
84
85
# File 'lib/waz/blobs/container.rb', line 83

def 
  self.class.service_instance.get_container_properties(self.name)
end

#public_access=(value) ⇒ Object

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



104
105
106
# File 'lib/waz/blobs/container.rb', line 104

def public_access=(value)
  self.class.service_instance.set_container_acl(self.name, value)
end

#public_access?Boolean

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

Returns:

  • (Boolean)


99
100
101
# File 'lib/waz/blobs/container.rb', line 99

def public_access?
  self.class.service_instance.get_container_acl(self.name)
end

#put_properties!(properties = {}) ⇒ Object

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.



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

def put_properties!(properties = {})
  self.class.service_instance.set_container_properties(self.name, properties)
end

#store(blob_name, payload, content_type, options = {}) ⇒ Object

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



116
117
118
119
120
121
122
# File 'lib/waz/blobs/container.rb', line 116

def store(blob_name, payload, content_type, options = {})
  blob_name.gsub!(%r{^/}, '')
  self.class.service_instance.put_blob("#{self.name}/#{blob_name}", payload, content_type, options)
  return BlobObject.new(:name => blob_name, 
                        :url => self.class.service_instance.generate_request_uri("#{self.name}/#{blob_name}"),
                        :content_type => content_type)
end