aws-sdkでs3にファイルをアップロードする時、マルチパートアップロードだとEtagがmd5にならない。
なので、Transform経由で横取りして算出する。
digeststream.js
'use strict';
var util = require('util');
var Transform = require('stream').Transform;
var createHash = require('crypto').createHash;
function DigestStream(algorithm, opts) {
if (!(this instanceof DigestStream)) {
return new DigestStream(algorithm, opts);
}
opts = opts || {};
opts.objectMode = false;
Transform.call(this, opts);
this._hash = createHash(algorithm);
}
util.inherits(DigestStream, Transform);
DigestStream.prototype._transform = function(chunk, encoding, callback) {
this.push(chunk);
this._hash.update(chunk);
callback();
};
DigestStream.prototype._flush = function(callback) {
this.emit('digest', this._hash.digest('hex'));
callback();
};
module.exports = DigestStream;
#使用例
app.js
var fs = require('fs');
var zlib = require('zlib');
var AWS = require('aws-sdk');
var config = require('./config.json');
var s3 = new AWS.S3(config.s3);
var DigestStream = require('./digeststream.js');
var digestStream = new DigestStream('md5');
digestStream.on('digest', function(value) {
console.log(value);
});
var stream = fs.createReadStream('./bigdata').pipe(zlib.createGzip()).pipe(digestStream);
var params = {
Bucket: 'bucket',
Key: 'key',
Body: stream
};
s3.upload(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});