The OSS SDK for Java uses MD5 verification and CRC-64 to ensure data integrity when you upload, download, and copy objects.

MD5 verification

If you configure Content-MD5 in an object upload request, OSS calculates the MD5 hash of the uploaded object. If the calculated MD5 hash is inconsistent with that configured in the uploaded request, InvalidDigest is returned. This allows OSS to ensure data integrity in the uploaded object.

The following code provides an example on how to perform MD5 verification in object uploads:

# -*- coding: utf-8 -*-
import oss2

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
object_name = '<yourObjectName>'
content = '<yourContent>'

# Calculate the MD5 hash of the uploaded content.
content_md5 = oss2.utils.content_md5(content);
print('content_md5', content_md5)

# If the upload request contains the Content-MD5 header, the OSS server verifies the MD5 hash of the uploaded content.
headers = dict()
headers['Content-MD5'] = content_md5
bucket.put_object(object_name, content, headers=headers)
Note MD5 verification can be used for put_object, append_object, post_object, and upload_part operations.

CRC-64

Note
  • CRC-64 can be used for put_object, get_object, append_object, and upload_part operations. CRC-64 is enabled by default when you upload an object. If the CRC-64 checksum calculated on the client is different from the CRC-64 checksum returned by the OSS server, the InconsistentError error code is returned.
  • CRC-64 is not supported in range download.
  • CRC-64 consumes CPU resources and slows down uploads and downloads.
  • CRC-64 in object downloads
    # -*- coding: utf-8 -*-
    import oss2
    
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
    auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
    # This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
    bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
    object_name = '<yourObjectName>'
    
    # Check whether CRC-64 is enabled by default.
    print('bucket.enable-crc:',  bucket.enable_crc)
    
    # The return value of bucket.get_object is a file-like and iterable object.
    object_stream = bucket.get_object(object_name)
    print(object_stream.read())
    
    # You must call read() to read the object from the stream returned by get_object before you can calculate the CRC-64 checksum of the object.
    if object_stream.client_crc ! = object_stream.server_crc:
        print "The CRC checksum between client and server is inconsistent!"
  • CRC-64 in append uploads

    If you set init_crc in append upload, CRC-64 is enabled by default.

    # -*- coding: utf-8 -*-
    import oss2
    
    # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com.
    auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
    # This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
    bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
    
    object_name = "<yourAppendObjectName>"
    first_content = "<yourFirstContent>"
    second_content = "<yourSecondContent>"
    
    # Set init_crc in the first append upload.
    # When init_crc is set, the OSS SDK for Python performs CRC-64 for the returned result.
    result = bucket.append_object(object_name, 0, first_content, init_crc=0)
    
    # Set init_crc in the second append upload.
    # Set init_crc to the CRC-64 checksum of the uploaded object.
    result = bucket.append_object(object_name, result.next_position, second_content, init_crc=result.crc)