Class: WAZ::Blobs::Container
- Inherits:
-
Object
- Object
- WAZ::Blobs::Container
- 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
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
.create(name) ⇒ Object
Creates a new container with the given name.
-
.find(name) ⇒ Object
Finds a container by name.
-
.list(options = {}) ⇒ Object
Returns all the containers on the given account.
-
.service_instance ⇒ Object
This method is internally used by this class.
Instance Method Summary collapse
-
#[](blob_name) ⇒ Object
Retrieves the blob by the given name.
-
#blobs ⇒ Object
Returns a list of blobs (WAZ::Blobs::BlobObject) contained on the current container.
-
#destroy! ⇒ Object
Removes the container from the current account.
-
#initialize(options = {}) ⇒ Container
constructor
Creates a new instance of the WAZ::Blobs::Container.
-
#metadata ⇒ Object
Returns the container metadata.
-
#public_access=(value) ⇒ Object
Sets a value indicating whether the container is public accessible (i.e. from a Web Browser) or not.
-
#public_access? ⇒ Boolean
Retuns a value indicating whether the container is public accessible (i.e. from a Web Browser) or not.
-
#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.
-
#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.
-
#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.
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( = {}) raise WAZ::Storage::InvalidOption, :name unless .keys.include?(:name) and ![:name].empty? self.name = [:name] end |
Instance Attribute Details
#name ⇒ Object
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( = {}) service_instance.list_containers().map { |container| Container.new(container) } end |
.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!
67 68 69 70 |
# File 'lib/waz/blobs/container.rb', line 67 def service_instance = WAZ::Storage::Base.default_connection.merge(:type_of_service => "blob") (@service_instances ||= {})[[:account_name]] ||= Service.new() 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.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/waz/blobs/container.rb', line 142 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 |
#blobs ⇒ Object
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 |
#metadata ⇒ Object
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.
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, = {}) blob_name.gsub!(%r{^/}, '') self.class.service_instance.put_blob("#{self.name}/#{blob_name}", payload, content_type, ) 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>.
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/waz/blobs/container.rb', line 128 def upload(blob_name, stream, content_type, = {}, 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, return BlobObject.new(:name => blob_name, :url => self.class.service_instance.generate_request_uri("#{self.name}/#{blob_name}"), :content_type => content_type) end |