Reputation: 704
S3 out of the box provides the MD5 checksum of the S3 object content. But I need to calculate the SHA-256 checksum of the file content. The file could be large enough so I do not want to load the file in memory and calculate the checksum, instead I need a solution to calculate the checksum without loading the whole file in memory.
Upvotes: 2
Views: 6055
Reputation: 704
It can be achieved by following steps in Java:
Following is the snippet on how to do it:
String getS3FileHash(AmazonS3 amazonS3, String s3bucket, String filePath) {
try {
InputStream inputStream = amazonS3.getObject(s3bucket, filePath).getObjectContent();
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
DigestInputStream digestInputStream = new DigestInputStream(inputStream, messageDigest);
byte[] buffer = new byte[4096];
int count = 0;
while (digestInputStream.read(buffer) > -1) {
count++;
}
log.info("total read: " + count);
MessageDigest digest = digestInputStream.getMessageDigest();
digestInputStream.close();
byte[] md5 = digest.digest();
StringBuilder sb = new StringBuilder();
for (byte b: md5) {
sb.append(String.format("%02X", b));
}
return sb.toString().toLowerCase();
} catch (Exception e) {
log.error(e);
}
return null;
}
Upvotes: 2