Skip to content

Instantly share code, notes, and snippets.

@amalgjose
Created January 3, 2016 07:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amalgjose/c95199ffe2e28bac67c1 to your computer and use it in GitHub Desktop.
Save amalgjose/c95199ffe2e28bac67c1 to your computer and use it in GitHub Desktop.
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