Using CRC64 to Verify File Consistency During Uploads
You can use CRC64 to verify file consistency during uploads through the following APIs:
- Uploading an Object - File-Based (SDK for Java)
- Uploading an Object - Append (SDK for Java)
- Multipart Upload (SDK for Java)
- Uploading an Object - Resumable (SDK for Java)
Sample Code 1: Uploading an Object - File-Based
The following code shows how to use CRC64 to verify data consistency during an object upload:
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 |
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.PutObjectRequest; import com.obs.services.model.PutObjectResult; import java.io.File; import java.util.Map; public class PutObjectWithCRC64{ 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 { // Upload a local file. String exampleBucket = "examplebucket"; String localFilePath = "localfile"; String exampleObjectKey = "objectKey"; PutObjectRequest putObjectRequest = new PutObjectRequest(exampleBucket, exampleObjectKey, new File(localFilePath)); putObjectRequest.setNeedCalculateCRC64(true); PutObjectResult putObjectResult = obsClient.putObject(putObjectRequest); System.out.println( "Server returned crc64 string:" + putObjectResult.getResponseHeaders().get(HASH_CRC64ECMA)); System.out.println("PutObject successfully"); } catch (ObsException e) { System.out.println("PutObject 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("PutObject failed"); // Print other error information. e.printStackTrace(); } } } |
Sample Code 2: Uploading an Object - Append
CRC64 can be used to verify file consistency only during the first append upload.
The following code shows how to use CRC64 to verify data consistency during an append upload:
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 |
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.AppendObjectRequest; import com.obs.services.model.AppendObjectResult; import java.io.File; import java.util.Map; public class AppendObjectWithCRC64{ 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 { // Append data. CRC64 verification is supported only for the first append upload. String exampleBucket = "examplebucket"; String localFilePath = "localfile"; String exampleObjectKey = "objectKeyAppend"; AppendObjectRequest appendObjectRequest = new AppendObjectRequest(exampleBucket); appendObjectRequest.setObjectKey(exampleObjectKey); appendObjectRequest.setFile(new File(localFilePath)); appendObjectRequest.setNeedCalculateCRC64(true); AppendObjectResult appendObjectResult = obsClient.appendObject(appendObjectRequest); System.out.println( "Server returned crc64 string:" + appendObjectResult.getResponseHeaders().get(HASH_CRC64ECMA)); System.out.println("AppendObject 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(); } } } |
Sample Code 3: Multipart Upload
The following code shows how to use CRC64 to verify data consistency during multipart uploads:
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
import com.obs.services.ObsClient; import com.obs.services.exception.ObsException; import com.obs.services.internal.utils.CRC64; import com.obs.services.model.CompleteMultipartUploadRequest; import com.obs.services.model.CompleteMultipartUploadResult; import com.obs.services.model.InitiateMultipartUploadRequest; import com.obs.services.model.InitiateMultipartUploadResult; import com.obs.services.model.PartEtag; import com.obs.services.model.UploadPartRequest; import com.obs.services.model.UploadPartResult; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Map; import static com.obs.services.internal.Constants.CommonHeaders.HASH_CRC64ECMA; public class CompleteMultiPartUploadWithCRC64{ 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 { // Perform a multipart upload. String exampleBucket = "examplebucket"; String localFilePath = "localfile"; String exampleObjectKey = "objectKey"; File exampleFile = new File(localFilePath); long exampleFileSize = exampleFile.length(); long partSize1 = exampleFileSize / 2; long partSize2 = exampleFileSize - partSize1; InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(exampleBucket, exampleObjectKey); InitiateMultipartUploadResult initiateMultipartUploadResult = obsClient.initiateMultipartUpload(initiateMultipartUploadRequest); String uploadId = initiateMultipartUploadResult.getUploadId(); UploadPartRequest uploadPartRequest = new UploadPartRequest(exampleBucket, exampleObjectKey, exampleFile); uploadPartRequest.setUploadId(uploadId); uploadPartRequest.setNeedCalculateCRC64(true); uploadPartRequest.setPartNumber(1); uploadPartRequest.setPartSize(partSize1); UploadPartResult uploadPartResult1 = obsClient.uploadPart(uploadPartRequest); CRC64 crc641 = uploadPartResult1.getClientCalculatedCRC64(); System.out.println( "Client calculated crc64_1 string:" + crc641.toString()); uploadPartRequest.setPartNumber(2); uploadPartRequest.setFile(exampleFile); uploadPartRequest.setOffset(partSize1); uploadPartRequest.setPartSize(partSize2); UploadPartResult uploadPartResult2 = obsClient.uploadPart(uploadPartRequest); CRC64 crc642 = uploadPartResult2.getClientCalculatedCRC64(); System.out.println( "Client calculated crc64_2 string:" + crc642.toString()); List<PartEtag> eTags = new ArrayList<>(); eTags.add(new PartEtag(uploadPartResult1.getEtag(), 1)); eTags.add(new PartEtag(uploadPartResult2.getEtag(), 2)); CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(exampleBucket, exampleObjectKey ,uploadId ,eTags); CRC64 crc64Combined = new CRC64(CRC64.combine(crc641.getValue(), crc642.getValue(), partSize2)); completeMultipartUploadRequest.addUserHeaders("x-obs-" + HASH_CRC64ECMA, crc64Combined.toString()); CompleteMultipartUploadResult completeMultipartUploadResult = obsClient.completeMultipartUpload(completeMultipartUploadRequest); System.out.println("Server returned completeMultipartUpload crc64 string:" + completeMultipartUploadResult.getResponseHeaders().get(HASH_CRC64ECMA)); System.out.println("CompleteMultipartUpload 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(); } } } |
Sample Code 4: Uploading an Object - Resumable
The following code shows how to use CRC64 to verify data consistency during a resumable upload:
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 |
import com.obs.services.ObsClient; import com.obs.services.exception.ObsException; import com.obs.services.model.CompleteMultipartUploadResult; import com.obs.services.model.UploadFileRequest; public class UploadFileWithCRC64{ 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 { UploadFileRequest request = new UploadFileRequest("examplebucket", "objectKey"); // Configure the local file to be uploaded. localfile is the path of the file to be uploaded. You must specify a file name with the extension. request.setUploadFile("localfile"); // Set the maximum number of parts that can be concurrently uploaded. request.setTaskNum(5); // Set the part size to 10 MB. request.setPartSize(10 * 1024 * 1024); // Enable the resumable upload. request.setEnableCheckpoint(true); // Enable CRC64 verification for the file. request.setNeedCalculateCRC64(true); // Perform a resumable upload. CompleteMultipartUploadResult result = obsClient.uploadFile(request); System.out.println("Server returned crc64 string:" + result.getResponseHeaders().get(HASH_CRC64ECMA)); System.out.println("UploadFile successfully"); } catch (ObsException e) { // If there is an exception, you can call the API again to resume the upload. System.out.println("UploadFile 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()); e.printStackTrace(); } catch (Exception e) { System.out.println("UploadFile failed"); // Print other error information. e.printStackTrace(); } } } |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot