Updated on 2025-12-11 GMT+08:00

Obtaining CRC64 of an Object

You can call the following APIs to obtain the CRC64 value of an object and compare it with the locally calculated CRC64 value to verify data consistency:

Sample Code 1: Downloading an Object - Streaming

The following code shows how to obtain the CRC64 value when using streaming to download an object:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import static com.obs.services.internal.Constants.CommonHeaders.HASH_CRC64ECMA;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.GetObjectRequest;
import com.obs.services.model.ObsObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class GetObjectWithCRC64{
    public static void main(String[] args) {
        // Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
        // Obtain an AK/SK pair on the management console.
        String ak = System.getenv("ACCESS_KEY_ID");
        String sk = System.getenv("SECRET_ACCESS_KEY_ID");
        // (Optional) If you are using a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage.
        // Obtain an AK/SK pair and a security token using environment variables or import them in other ways.
        // String securityToken = System.getenv("SECURITY_TOKEN");
        // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
        String endPoint = "https://obs.ap-southeast-1.myhuaweicloud.com";
        // Obtain an endpoint using environment variables or import it in other ways.
        //String endPoint = System.getenv("ENDPOINT");
        
        // Create an ObsClient instance.
        // Use the permanent AK/SK pair to initialize the client.
        ObsClient obsClient = new ObsClient(ak, sk,endPoint);
        // Use the temporary AK/SK pair and security token to initialize the client.
        // ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint);

        try {
            // Download an object.
            String exampleBucket = "examplebucket";
            String exampleObjectKey = "objectKey";
            GetObjectRequest getObjectRequest = new GetObjectRequest(exampleBucket, exampleObjectKey);
            ObsObject obsObject = obsClient.getObject(getObjectRequest);
            InputStream inputStream = obsObject.getObjectContent();
            System.out.println("Server returned crc64 string:" + obsObject.getMetadata().getCrc64());
            System.out.println("GetObject successfully");
        } catch (ObsException e) {
            System.out.println("GetObject failed");
            // Request failed. Print the HTTP status code.
            System.out.println("HTTP Code:" + e.getResponseCode());
            // Request failed. Print the server-side error code.
            System.out.println("Error Code:" + e.getErrorCode());
            // Request failed. Print the error details.
            System.out.println("Error Message:" + e.getErrorMessage());
            // Request failed. Print the request ID.
            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
            // In an abnormal scenario, print all headers, which may contain error information.
            Map<String, String> headers = e.getResponseHeaders();
            if (headers != null) {
                for (Map.Entry<String, String> header : headers.entrySet()) {
                    System.out.println(header.getKey() + ":" + header.getValue());
                }
            }
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("GetObject failed");
            // Print other error information.
            e.printStackTrace();
        }
    }
}

Sample Code 2: Downloading an Object - Range-Based

The following code shows how to obtain the CRC64 value when downloading an object by specifying a range.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import static com.obs.services.internal.Constants.CommonHeaders.HASH_CRC64ECMA;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.GetObjectRequest;
import com.obs.services.model.ObsObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class GetObjectWithCRC64{
    public static void main(String[] args) {
        // Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
        // Obtain an AK/SK pair on the management console.
        String ak = System.getenv("ACCESS_KEY_ID");
        String sk = System.getenv("SECRET_ACCESS_KEY_ID");
        // (Optional) If you are using a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage.
        // Obtain an AK/SK pair and a security token using environment variables or import them in other ways.
        // String securityToken = System.getenv("SECURITY_TOKEN");
        // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
        String endPoint = "https://obs.ap-southeast-1.myhuaweicloud.com";
        // Obtain an endpoint using environment variables or import it in other ways.
        //String endPoint = System.getenv("ENDPOINT");
        
        // Create an ObsClient instance.
        // Use the permanent AK/SK pair to initialize the client.
        ObsClient obsClient = new ObsClient(ak, sk,endPoint);
        // Use the temporary AK/SK pair and security token to initialize the client.
        // ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint);

        try {
            // Download an object.
            String exampleBucket = "examplebucket";
            String exampleObjectKey = "objectKey";
            GetObjectRequest getObjectRequest = new GetObjectRequest(exampleBucket, exampleObjectKey);
            // Set the start point and end point.
            getObjectRequest.setRangeStart(0l);
            getObjectRequest.setRangeEnd(1000l);
            ObsObject obsObject = obsClient.getObject(getObjectRequest);
            InputStream inputStream = obsObject.getObjectContent();
            System.out.println("Server returned crc64 string:" + obsObject.getMetadata().getCrc64());
            System.out.println("GetObject successfully");
        } catch (ObsException e) {
            System.out.println("GetObject failed");
            // Request failed. Print the HTTP status code.
            System.out.println("HTTP Code:" + e.getResponseCode());
            // Request failed. Print the server-side error code.
            System.out.println("Error Code:" + e.getErrorCode());
            // Request failed. Print the error details.
            System.out.println("Error Message:" + e.getErrorMessage());
            // Request failed. Print the request ID.
            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
            // In an abnormal scenario, print all headers, which may contain error information.
            Map<String, String> headers = e.getResponseHeaders();
            if (headers != null) {
                for (Map.Entry<String, String> header : headers.entrySet()) {
                    System.out.println(header.getKey() + ":" + header.getValue());
                }
            }
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("GetObject failed");
            // Print other error information.
            e.printStackTrace();
        }
    }
}

Sample Code 3: Obtaining Object Metadata

The following code shows how to obtain the CRC64 value of an object:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import static com.obs.services.internal.Constants.CommonHeaders.HASH_CRC64ECMA;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.ObjectMetadata;

import java.util.Map;

public class GetObjectMetaDataWithCRC64{
    public static void main(String[] args) {
        // Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
        // Obtain an AK/SK pair on the management console.
        String ak = System.getenv("ACCESS_KEY_ID");
        String sk = System.getenv("SECRET_ACCESS_KEY_ID");
        // (Optional) If you are using a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage.
        // Obtain an AK/SK pair and a security token using environment variables or import them in other ways.
        // String securityToken = System.getenv("SECURITY_TOKEN");
        // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
        String endPoint = "https://obs.ap-southeast-1.myhuaweicloud.com";
        // Obtain an endpoint using environment variables or import it in other ways.
        //String endPoint = System.getenv("ENDPOINT");
        
        // Create an ObsClient instance.
        // Use the permanent AK/SK pair to initialize the client.
        ObsClient obsClient = new ObsClient(ak, sk,endPoint);
        // Use the temporary AK/SK pair and security token to initialize the client.
        // ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint);

        try {
            // Obtain the object metadata.
            String exampleBucket = "examplebucket";
            String exampleObjectKey = "objectKey";
            ObjectMetadata objectMetadata = obsClient.getObjectMetadata(exampleBucket, exampleObjectKey);
            System.out.println(
                    "Server returned crc64 string:" + objectMetadata.getCrc64());
            System.out.println("GetObject successfully");
        } catch (ObsException e) {
            System.out.println("GetObject failed");
            // Request failed. Print the HTTP status code.
            System.out.println("HTTP Code:" + e.getResponseCode());
            // Request failed. Print the server-side error code.
            System.out.println("Error Code:" + e.getErrorCode());
            // Request failed. Print the error details.
            System.out.println("Error Message:" + e.getErrorMessage());
            // Request failed. Print the request ID.
            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
            // In an abnormal scenario, print all headers, which may contain error information.
            Map<String, String> headers = e.getResponseHeaders();
            if (headers != null) {
                for (Map.Entry<String, String> header : headers.entrySet()) {
                    System.out.println(header.getKey() + ":" + header.getValue());
                }
            }
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("GetObject failed");
            // Print other error information.
            e.printStackTrace();
        }
    }
}

Sample Code 4: Copying an Object

The following code shows how to obtain the CRC64 value when copying an object:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.CopyObjectRequest;
import com.obs.services.model.CopyObjectResult;

import java.util.Map;
public class CopyObjectWithCRC64{
    public static void main(String[] args) {
        // Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
        // Obtain an AK/SK pair on the management console.
        String ak = System.getenv("ACCESS_KEY_ID");
        String sk = System.getenv("SECRET_ACCESS_KEY_ID");
        // (Optional) If you are using a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage.
        // Obtain an AK/SK pair and a security token using environment variables or import them in other ways.
        // String securityToken = System.getenv("SECURITY_TOKEN");
        // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
        String endPoint = "https://obs.ap-southeast-1.myhuaweicloud.com";
        // Obtain an endpoint using environment variables or import it in other ways.
        //String endPoint = System.getenv("ENDPOINT");
        
        // Create an ObsClient instance.
        // Use the permanent AK/SK pair to initialize the client.
        ObsClient obsClient = new ObsClient(ak, sk,endPoint);
        // Use the temporary AK/SK pair and security token to initialize the client.
        // ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint);

        try {
            // Copy a file.
            String exampleSourceBucket = "examplebucket";
            String exampleDestinationBucket = "destinationexamplebucket";
            String exampleSourceObjectKey = "objectKey";
            String exampleDestinationObjectKey = "copyObjectKey";
            CopyObjectRequest copyObjectRequest =
                    new CopyObjectRequest(
                            exampleSourceBucket,
                            exampleSourceObjectKey,
                            exampleDestinationBucket,
                            exampleDestinationObjectKey);
            CopyObjectResult copyObjectResult = obsClient.copyObject(copyObjectRequest);
            String crc64 = copyObjectResult.getCRC64();
            System.out.println("Server returned crc64 string:" + crc64);
            System.out.println("CopyObject successfully");
        } catch (ObsException e) {
            System.out.println("AppendObject failed");
            // Request failed. Print the HTTP status code.
            System.out.println("HTTP Code:" + e.getResponseCode());
            // Request failed. Print the server-side error code.
            System.out.println("Error Code:" + e.getErrorCode());
            // Request failed. Print the error details.
            System.out.println("Error Message:" + e.getErrorMessage());
            // Request failed. Print the request ID.
            System.out.println("Request ID:" + e.getErrorRequestId());
            System.out.println("Host ID:" + e.getErrorHostId());
            // In an abnormal scenario, print all headers, which may contain error information.
            Map<String, String> headers = e.getResponseHeaders();
            if (headers != null) {
                for (Map.Entry<String, String> header : headers.entrySet()) {
                    System.out.println(header.getKey() + ":" + header.getValue());
                }
            }
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("AppendObject failed");
            // Print other error information.
            e.printStackTrace();
        }
    }
}