Skip to content

Instantly share code, notes, and snippets.

@jed
Last active March 5, 2018 13:36
Show Gist options
  • Save jed/23f5fee4abae6b7255068265ac8d1f35 to your computer and use it in GitHub Desktop.
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.
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