Last active
March 5, 2018 13:36
-
-
Save jed/23f5fee4abae6b7255068265ac8d1f35 to your computer and use it in GitHub Desktop.
Create an etag for an S3 object. Assumes object is uploaded with exact chunk size.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {createReadStream} = require('fs') | |
const {createHash} = require('crypto') | |
module.exports = (path, chunkSize = 5242880) => { | |
return new Promise((resolve, reject) => { | |
const hashes = [] | |
const options = {highWaterMark: chunkSize} | |
const rs = createReadStream(path, options) | |
rs.on('error', reject) | |
rs.on('data', data => { | |
const hash = createHash('md5').update(data).digest() | |
hashes.push(hash) | |
}) | |
rs.on('end', () => { | |
if (hashes.length < 2) return resolve(hashes[0].toString('hex')) | |
const data = Buffer.concat(hashes) | |
const hash = createHash('md5').update(data).digest('hex') | |
resolve(`${hash}-${hashes.length}`) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment