TL;DR don't send the md5.
If you don't mind reading the stream twice, you can calculate the md5 beforehand like this:
var fs = require('fs');
var crypto = require('crypto');
function calculateStreamHash(readableStream, algorithm, encoding, cb) {
var hash = crypto.createHash(algorithm);
readableStream.on('data', updateHash);
readableStream.on('end', finish);
function updateHash(d) {
hash.update(d);
}
function finish() {
var d = hash.digest(encoding);
cb(d);
}
}
calculateStreamHash(fs.createReadStream('/tmp/' + filename), 'md5', 'base64', function(md5) {
// use md5
});
If reading the stream twice is not an option, I suggest not sending an MD5 at all. Looking at the Manta's Node.js SDK it seems to be an optional field. See https://github.com/joyent/node-manta/blob/master/lib/client.js#L169
In fact, it looks like that in the specific method you're using createWriteStream()
, the md5
option field is completely ignored. See https://github.com/joyent/node-manta/blob/master/lib/client.js#L1399
So you should probably ignore that md5
field.