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.

The following examples use a string "123456789" to show how to calculate the Content-MD5 value of the request content:

  • Correct calculation
    1. Calculate the MD5 hash of the string, which is a 128-bit binary array.
    2. Encode the binary array (instead of the 32-bit string) in Base64.
    The following Python code provides an example on how to calculate the Content-MD5 value:
    >>> import base64,hashlib
    >>> hash = hashlib.md5()
    >>> hash.update("0123456789")   // If you use Python 3, change the code to hash.update(b"0123456789"). 
    >>> base64.b64encode(hash.digest())

    Call hash.digest() to calculate the 128-bit binary array.

    >>> hash.digest()
  • Incorrect calculation
    Note A common incorrect operation is to encode the calculated 32-bit string in Base64 to obtain the Content-MD5 value.
    # Call hash.hexdigest() to obtain a 32-bit plaintext string. 
    >>> hash.hexdigest()
    # The following code provides an example on encoding the incorrect MD5 hash in Base64: 
    >>> base64.b64encode(hash.hexdigest())