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.



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/waz/blobs/container.rb', line 153

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],
                          :railsetag => properties[:x_ms_meta_railsetag])
  rescue RestClient::ResourceNotFound
    return nil
  end
end

#blobsObject

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



120
121
122
# File 'lib/waz/blobs/container.rb', line 120

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.



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

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.



115
116
117
# File 'lib/waz/blobs/container.rb', line 115

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)


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

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.



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

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

#statistics(options) ⇒ Hash

Returns statistics of the container.

Parameters:

  • options (Hash)

Options Hash (options):

  • :maxresults (String)

    max blobs(5,000 at most)

  • :marker (String)

    marker of a page(“2!80!MDAwMDE0***********–”)

Returns:

  • (Hash)

    => Integer, :files => Integer, :marker => String



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

def statistics(options)
  self.class.service_instance.statistics(self.name, options)
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>.



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

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

#upload(blob_name, stream, content_type, options = {}, block_size = 4 * 2**20) ⇒ Object

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



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/waz/blobs/container.rb', line 139

def upload(blob_name, stream, content_type, options = {}, block_size = 4 * 2**20)
  blob_name.gsub!(%r{^/}, '')
  path = "#{self.name}/#{blob_name}"
  n = 0
  until stream.eof?
    self.class.service_instance.put_block path, Base64.encode64('%064d' % n), stream.read(block_size)
    n += 1
  end
  self.class.service_instance.put_block_list path, (0...n).map{|id| Base64.encode64('%064d' % id)}, 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