Skip to content

Instantly share code, notes, and snippets.

@robbyt
Created February 23, 2012 23:14
Show Gist options
  • Save robbyt/1895635 to your computer and use it in GitHub Desktop.
Save robbyt/1895635 to your computer and use it in GitHub Desktop.
boto etag hash auto verification
import boto
s3 = boto.connect_s3()
bucket = s3.get_bucket('robs-super-bucket')
d = bucket.new_key()
file1 = open('testfile.txt', 'r')
file2 = open('otherfile.txt', 'r')
# build the hash from file1
d.etag = d.compute_md5(file)
# but upload file2... <kaboom!>
d.send_file(file2)
# If you set the etag header before uploading a file to s3 using boto,
# s3 will compute the md5 on their side and reject the upload if the
# sum on their side does not match the sum that you set prior to upload
#
# e.g.,
# S3DataError: BotoClientError: ETag from S3 did not match computed MD5
@Atothendrew
Copy link

Thanks for this gist. Should line 11 be d.etag = d.compute_md5(file1) though?

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