Method: OpenStack::Swift::StorageObject#data

Defined in:
lib/openstack/swift/storage_object.rb

#data(size = -1,, offset = 0, headers = {}) ⇒ Object Also known as: read

Retrieves the data from an object and stores the data in memory. The data is returned as a string. Throws a OpenStack::Exception::ItemNotFound if the object doesn’t exist.

If the optional size and range arguments are provided, the call will return the number of bytes provided by size, starting from the offset provided in offset.

object.data
=> "This is the text stored in the file"


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/openstack/swift/storage_object.rb', line 126

def data(size = -1, offset = 0, headers = {})
  headers = {'content-type'=>'application/json'}
  if size.to_i > 0
    range = sprintf("bytes=%d-%d", offset.to_i, (offset.to_i + size.to_i) - 1)
    headers['Range'] = range
  end
  path = "/#{@containername}/#{@name}"
  begin
    response = @container.swift.connection.req("GET", URI.encode(path), {:headers=>headers})
    response.body
  rescue OpenStack::Exception::ItemNotFound => not_found
    msg = "No Object \"#{@name}\" found in Container \"#{@containername}\".  #{not_found.message}"
    raise OpenStack::Exception::ItemNotFound.new(msg, not_found.response_code, not_found.response_body)
  end
end