Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python program to find the checksum and Etag of a file. Etag is a property of a file in S3 which is similar to checksum
import os
import boto
import traceback
from boto.s3.connection import S3Connection
class ETagExample(object):
def __init__(self):
self.aws_access_key = "XXXXXXXXX"
self.aws_secret_key = "XXXXXXXXX"
self.s3_bucket = "checksum-test"
self.s3_conn = boto.connect_s3(aws_access_key_id=self.aws_access_key,
aws_secret_access_key=self.aws_secret_key,
calling_format=boto.s3.connection.OrdinaryCallingFormat())
#Function to calculate the checksum of a local file
def find_checksum(self, file_name):
try:
checksum = hashlib.md5(open(file_name).read()).hexdigest()
return checksum
except Exception, e:
print "Exception occurred while calculating checksum :" + str(e)
print traceback.print_exc()
#Function to calculate the Etag of a file in S3
def find_etag(self, full_key_name):
try:
bucket = self.s3_conn.get_bucket(self.s3_bucket)
key = bucket.new_key(full_key_name)
s3_etag = key.etag.strip('"').strip("'")
return s3_etag
except Exception, e:
print "Exception occurred while calculating S3 Etag : " + str(e)
print traceback.print_exc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment