Method: CloudFiles::StorageObject#write
- Defined in:
- lib/cloudfiles/storage_object.rb
#write(data = nil, headers = {}) ⇒ Object
Takes supplied data and writes it to the object, saving it. You can supply an optional hash of headers, including Content-Type and ETag, that will be applied to the object.
If you would rather stream the data in chunks, instead of reading it all into memory at once, you can pass an IO object for the data, such as: object.write(open(‘/path/to/file.mp3’))
You can compute your own MD5 sum and send it in the “ETag” header. If you provide yours, it will be compared to the MD5 sum on the server side. If they do not match, the server will return a 422 status code and a CloudFiles::Exception::MisMatchedChecksum Exception will be raised. If you do not provide an MD5 sum as the ETag, one will be computed on the server side.
Updates the container cache and returns true on success, raises exceptions if stuff breaks.
object = container.create_object("newfile.txt")
object.write("This is new data")
=> true
object.data
=> "This is new data"
If you are passing your data in via STDIN, just do an
object.write
with no data (or, if you need to pass headers)
object.write(nil,{'header' => 'value})
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/cloudfiles/storage_object.rb', line 219 def write(data = nil, headers = {}) raise CloudFiles::Exception::Syntax, "No data or header updates supplied" if ((data.nil? && $stdin.tty?) and headers.empty?) # If we're taking data from standard input, send that IO object to cfreq data = $stdin if (data.nil? && $stdin.tty? == false) begin response = SwiftClient.put_object(self.container.connection.storageurl, self.container.connection.authtoken, self.container.escaped_name, @name, data, nil, nil, nil, nil, headers) rescue ClientException => e code = e.status.to_s raise CloudFiles::Exception::InvalidResponse, "Invalid content-length header sent" if (code == "412") raise CloudFiles::Exception::MisMatchedChecksum, "Mismatched etag" if (code == "422") raise CloudFiles::Exception::InvalidResponse, "Invalid response code #{code}" unless (code =~ /^20./) end make_path(File.dirname(self.name)) if @make_path == true self.refresh true end |