new GridFSBucketWriteStream(bucket, filename, options){GridFSBucketWriteStream}
A writable stream that enables you to write buffers to GridFS.
Do not instantiate this class directly. Use openUploadStream()
instead.
Name | Type | Default | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
bucket |
GridFSBucket |
Handle for this stream's corresponding bucket |
|||||||||||||||||||||||||
filename |
string |
The value of the 'filename' key in the files doc |
|||||||||||||||||||||||||
options |
object | null |
optional
Optional settings.
|
Fires:
Returns:
GridFSBucketWriteStream instance.Methods
-
abort(callback){Promise}
-
Places this write stream into an aborted state (all future writes fail)
and deletes all chunks that have already been written.Name Type Description callback
GridFSBucket~errorCallback called when chunks are successfully removed or error occurred
Returns:
no callback specified
Example
// Aborting an upload var MongoClient = require('mongodb').MongoClient, test = require('assert'); MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { var bucket = new GridFSBucket(db, { bucketName: 'gridfsabort', chunkSizeBytes: 1 }); var CHUNKS_COLL = 'gridfsabort.chunks'; var FILES_COLL = 'gridfsabort.files'; var uploadStream = bucket.openUploadStream('test.dat'); var id = uploadStream.id; var query = { files_id: id }; uploadStream.write('a', 'utf8', function(error) { test.equal(error, null); db.collection(CHUNKS_COLL).count(query, function(error, c) { test.equal(error, null); test.equal(c, 1); uploadStream.abort(function(error) { test.equal(error, null); db.collection(CHUNKS_COLL).count(query, function(error, c) { test.equal(error, null); test.equal(c, 0); uploadStream.write('b', 'utf8', function(error) { test.equal(error.toString(), 'Error: this stream has been aborted'); uploadStream.end('c', 'utf8', function(error) { test.equal(error.toString(), 'Error: this stream has been aborted'); // Fail if user tries to abort an aborted stream uploadStream.abort().then(null, function(error) { test.equal(error.toString(), 'Error: Cannot call abort() on a stream twice'); }); }); }); }); }); }); }); });
-
end(chunk, encoding, callback)
-
Tells the stream that no more data will be coming in. The stream will
persist the remaining data to MongoDB, write the files document, and
then emit a 'finish' event.Name Type Description chunk
Buffer Buffer to write
encoding
String Optional encoding for the buffer
callback
function Function to call when all files and chunks have been persisted to MongoDB
-
write(chunk, encoding, callback){Boolean}
-
Write a buffer to the stream.
Name Type Description chunk
Buffer Buffer to write
encoding
String Optional encoding for the buffer
callback
function Function to call when the chunk was added to the buffer, or if the entire chunk was persisted to MongoDB if this chunk caused a flush.
Returns:
if this write required flushing a chunk to MongoDB. True otherwise.
Events
-
An error occurred
Type:
- Error
-
finish
-
end()
was called and the write stream successfully wrote the file
metadata and all the chunks to MongoDB.Type:
- object