Using MD5 to Verify File Consistency During Uploads (SDK for Java)
You can use MD5 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)
- Uploading a Part (SDK for Java)
Sample Code 1: Uploading an Object - File-Based
This example uploads local file localimage.jpg to bucket examplebucket as object objectname and calls ObsClient.base64Md5 to calculate the Content-MD5 header.
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 | import com.obs.services.ObsClient; import com.obs.services.exception.ObsException; import com.obs.services.model.ObjectMetadata; import java.io.File; import java.nio.file.Files; import java.nio.file.Paths; public class PutObject009 { 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 a permanent AK/SK pair to initialize the client. ObsClient obsClient = new ObsClient(ak, sk, endPoint); // Use a temporary AK/SK pair and security token to initialize the client. // ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint); try { // Set the MD5 value for an object. // Upload an image. ObjectMetadata metadata = new ObjectMetadata(); // Your file's Base64-encoded MD5 String base64Md5 = obsClient.base64Md5(Files.newInputStream(Paths.get("localimage.jpg"))); metadata.setContentMd5(base64Md5); obsClient.putObject("examplebucket", "objectname", new File("localimage.jpg"), metadata); 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()); e.printStackTrace(); } catch (Exception e) { System.out.println("putObject failed"); // Print other error information. e.printStackTrace(); } } } |
Sample Code 2: Uploading an Object - Append
The following code shows how to use MD5 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 67 68 69 70 71 | import com.obs.services.ObsClient; import com.obs.services.exception.ObsException; import com.obs.services.model.AppendObjectRequest; import com.obs.services.model.AppendObjectResult; import com.obs.services.model.ObjectMetadata; import java.io.ByteArrayInputStream; public class AppendObjectWithMD5{ 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 a permanent AK/SK pair to initialize the client. ObsClient obsClient = new ObsClient(ak, sk, endPoint); // Use a temporary AK/SK pair and security token to initialize the client. // ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint); try { String content = "Hello OBS"; // Use the MD5 algorithm provided by the OBS client. String contentMD5 = obsClient.base64Md5(new ByteArrayInputStream(content.getBytes())); System.out.println("Content MD5 calculated: " + contentMD5); AppendObjectRequest request = new AppendObjectRequest(); request.setBucketName("ztw-test11"); request.setObjectKey("objectname1111"); request.setPosition(0); // Send the MD5 value to the server for verification. ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentMd5(contentMD5); request.setMetadata(metadata); request.setInput(new ByteArrayInputStream("Hello OBS".getBytes())); // Append data to an object. obsClient.appendObject(request); 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: Uploading a Part
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 | import com.obs.services.ObsClient; import com.obs.services.exception.ObsException; import com.obs.services.internal.utils.CRC64; import com.obs.services.model.InitiateMultipartUploadRequest; import com.obs.services.model.InitiateMultipartUploadResult; import com.obs.services.model.UploadPartRequest; import com.obs.services.model.UploadPartResult; import java.io.File; import java.util.Map; import static com.obs.services.internal.Constants.CommonHeaders.HASH_CRC64ECMA; public class UploadPartWithMD5{ 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 a permanent AK/SK pair to initialize the client. ObsClient obsClient = new ObsClient(ak, sk, endPoint); // Use a temporary AK/SK pair and security token to initialize the client. // ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint); try { // Upload a part. String exampleBucket = "examplebucket"; String localFilePath = "localfile"; String exampleObjectKey = "objectKey"; File exampleFile = new File(localFilePath); 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); // Enable MD5 verification for the uploaded data. uploadPartRequest.setAttachMd5(true); uploadPartRequest.setPartNumber(1); uploadPartRequest.setPartSize(exampleFile.length()); obsClient.uploadPart(uploadPartRequest); System.out.println("UploadPart successfully"); } catch (ObsException e) { System.out.println("UploadPart 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("UploadPart 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