Skip to content

Instantly share code, notes, and snippets.

@rhossi
Created July 30, 2015 18:22
Show Gist options
  • Save rhossi/d32c7a0ebd15f8fec0e1 to your computer and use it in GitHub Desktop.
Save rhossi/d32c7a0ebd15f8fec0e1 to your computer and use it in GitHub Desktop.
Uploading files to S3 validating ContentMD5 using AWS SDK for Node.js
var aws = require('aws-sdk'),
fs = require('fs'),
crypt = require("crypto");
function getMD5HashFromFile(file){
var hash = crypt.createHash("md5")
.update(file)
.digest("base64");
return hash;
}
fs.readFile('demo-diebold.txt', function (err, data) {
if (err) { throw err; }
var fileData = new Buffer(data, 'binary');
var md5Hash = getMD5HashFromFile(data);
var s3 = new aws.S3();
s3.putObject({
Bucket: 'megasenateste',
Key: 'diebold-demo.txt',
Body: fileData,
ContentMD5: md5Hash
},function (resp) {
console.log(arguments);
});
});
@smber1
Copy link

smber1 commented Apr 12, 2019

Thank you for making my Friday morning just a little easier :)
new Buffer() is deprecated nowadays in favour of Buffer.from().

@AnkitGupta279073
Copy link

AnkitGupta279073 commented May 22, 2021

how can I use this S3 validating ContentMD5 using AWS SDK in pure javascript?

@chokri
Copy link

chokri commented Jul 19, 2021

Hi, how can I get the Hash from an uploaded file via ApiGateway (Base64). Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment