diff --git a/README.md b/README.md index a331b97956c..5cd5472dd5e 100644 --- a/README.md +++ b/README.md @@ -341,16 +341,21 @@ A bucket object allows you to write a readable stream, a file and a buffer as file contents. ~~~~ js -// Uploads file.pdf -bucket.writeFile( - filename, '/path/to/file.pdf', { contentType: 'application/pdf' }, callback); +// Uploads file.pdf. +bucket.write(name, { + filename: '/path/to/file.pdf', + metadata: { /* metadata properties */ } +}, callback); -// Reads the stream and uploads it as file contents -bucket.writeStream( - filename, fs.createReadStream('/path/to/file.pdf'), metadata, callback); +// Uploads the readable stream. +bucket.write(name, { + data: anyReadableStream, + metadata: { /* metadata properties */ } +}, callback); -// Uploads 'Hello World' as file contents -bucket.writeBuffer(filename, 'Hello World', callback); +// Uploads 'Hello World' as file contents. +// data could be any string or buffer. +bucket.write(name, { data: 'Hello World' }, callback); ~~~~ #### Copy files diff --git a/lib/storage/index.js b/lib/storage/index.js index 819fa24c5b6..a9e4fb4e213 100644 --- a/lib/storage/index.js +++ b/lib/storage/index.js @@ -254,17 +254,25 @@ Bucket.prototype.createReadStream = function(name) { /** * Writes the provided stream to the destination * with optional metadata. - * @param {String} name Name of the remote file. - * @param {Stream} stream A readable stream. - * @param {Object?} metadata Optional metadata. + * @param {String} name Name of the remote file. + * @param {Object=} opts.data A string, buffer or readable stream. + * @param {string=} opts.filename Path of the source file. + * @param {Object=} opts.metadata Optional metadata. * @param {Function} callback Callback function. */ -Bucket.prototype.writeStream = function(name, stream, metadata, callback) { - if (!callback) { - callback = metadata, metadata = {}; +Bucket.prototype.write = function(name, opts, callback) { + // TODO(jbd): Support metadata only requests. + var that = this; + + var metadata = opts.metadata || {}; + var stream = opts.data; + + if (opts.filename) { + stream = fs.createReadStream(opts.filename); + } else if (opts.data && (typeof opts.data === 'string' || opts.data instanceof Buffer)) { + stream = new BufferStream(opts.data); } - var that = this; var boundary = uuid.v4(); metadata.contentType = metadata.contentType || 'text/plain' this.conn.createAuthorizedReq({ @@ -298,34 +306,11 @@ Bucket.prototype.writeStream = function(name, stream, metadata, callback) { remoteStream.write('Content-Type: ' + metadata.contentType + '\n\n'); stream.pipe(remoteStream); // TODO(jbd): High potential of multiple callback invokes. - reqStreamToCallback(stream, callback); + stream.on('error', callback); reqStreamToCallback(remoteStream, callback); }); }; -/** - * Writes the source file to the destination with - * optional metadata. - * @param {String} name Name of the remote file. - * @param {String} filename Path to the source file. - * @param {object?} metadata Optional metadata. - * @param {Function} callback Callback function. - */ -Bucket.prototype.writeFile = function(name, filename, metadata, callback) { - this.writeStream(name, fs.createReadStream(filename), metadata, callback); -}; - -/** - * Writes the provided buffer to the destination file. - * @param {String} name Name of the remote file resource. - * @param {Buffer} buffer Buffer contents to be written. - * @param {Object?} metadata Optional metadata. - * @param {Function} callback Callback function. - */ -Bucket.prototype.writeBuffer = function(name, buffer, metadata, callback) { - this.writeStream(name, new BufferStream(buffer), metadata, callback); -}; - /** * Makes a new request object from the provided * arguments, and wraps the callback to intercept