Skip to content

Instantly share code, notes, and snippets.

@jed jed/s3-etag.js
Last active Mar 5, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.