Objects in Object Storage Service (OSS) have ETag values that are used to identify whether changes are made to data on the server. However, these ETag values are not necessarily equal to the MD5 hashes of the objects. We recommend that you do not use ETag values to verify data consistency.
To check whether an uploaded object in OSS is consistent with the local file, you can include the Content-MD5 header value in the upload request. When OSS receives the object, OSS compares the MD5 hash with the Content-MD5 header value. The object can be uploaded only when the MD5 hash is consistent with the Content-MD5 header value. This way, data consistency is ensured.
This section uses the message content "0123456789" as an example to show the correct and incorrect ways to calculate the Content-MD5 value.
-
Correct calculation
-
First, calculate the MD5 hash, which is a 128-bit binary array.
-
Base64-encode the binary array, not the 32-character hexadecimal string.
The following example uses Python:
>>> import base64,hashlib >>> hash = hashlib.md5() >>> hash.update("0123456789") # In Python 3, change this to hash.update(b"0123456789"). >>> base64.b64encode(hash.digest()) 'eB5e********************'The hash.digest() method returns the 128-bit binary array.
>>> hash.digest() 'x\x1e^$]i\xb5f\x97\x9b\x86\xe2\x8d#\xf2\xc7' -
-
Example of an incorrect calculation
NoteA common mistake is to directly Base64-encode the 32-character hexadecimal string.
# The hash.hexdigest() method calculates the visible 32-character hexadecimal string. >>> hash.hexdigest() '781e****************************' # The result of Base64-encoding the incorrect MD5 hash. >>> base64.b64encode(hash.hexdigest()) 'Nzgx****************************************'
Some header parameters, such as Content-MD5, must be included in the signature calculation. The header and the signature must match. Otherwise, a SignatureDoesNotMatch error is returned. For more information about the headers to sign, see Signature V1.