3030import  com .google .cloud .storage .multipartupload .model .CreateMultipartUploadResponse ;
3131import  com .google .cloud .storage .multipartupload .model .UploadPartRequest ;
3232import  com .google .cloud .storage .multipartupload .model .UploadPartResponse ;
33+ import  com .google .common .hash .HashCode ;
34+ import  com .google .common .hash .Hashing ;
3335import  java .io .IOException ;
3436import  java .io .InputStream ;
3537import  java .io .OutputStream ;
3840import  java .net .URI ;
3941import  java .net .URL ;
4042import  java .net .URLEncoder ;
43+ import  java .nio .ByteBuffer ;
4144import  java .nio .charset .StandardCharsets ;
4245import  java .security .MessageDigest ;
4346import  java .security .NoSuchAlgorithmException ;
@@ -134,11 +137,17 @@ public CompleteMultipartUploadResponse completeMultipartUpload(CompleteMultipart
134137
135138    MessageDigest  md  = MessageDigest .getInstance ("MD5" );
136139    String  contentMd5  = Base64 .getEncoder ().encodeToString (md .digest (xmlBodyBytes ));
140+ 
141+     HashCode  crc32cHash  = Hashing .crc32c ().hashBytes (xmlBodyBytes );
142+     byte [] crc32cBytes  = ByteBuffer .allocate (4 ).putInt (crc32cHash .asInt ()).array ();
143+     String  crc32cBase64  = Base64 .getEncoder ().encodeToString (crc32cBytes );
144+ 
137145    String  date  = getRfc1123Date ();
138146    String  contentType  = "application/xml" ;
147+     String  canonicalizedHeaders  = "x-goog-hash:crc32c="  + crc32cBase64  + "\n " ;
139148
140149    // GCS Signature Rule #3: The query string IS NOT included for the POST complete request. 
141-     String  signature  = signRequest ("POST" , contentMd5 , contentType , date , resourcePath , GOOGLE_SECRET_KEY );
150+     String  signature  = signRequest ("POST" , contentMd5 , contentType , date , canonicalizedHeaders ,  resourcePath , GOOGLE_SECRET_KEY );
142151    String  authHeader  = "GOOG1 "  + GOOGLE_ACCESS_KEY  + ":"  + signature ;
143152
144153    HttpURLConnection  connection  = (HttpURLConnection ) new  URL (uri ).openConnection ();
@@ -147,6 +156,7 @@ public CompleteMultipartUploadResponse completeMultipartUpload(CompleteMultipart
147156    connection .setRequestProperty ("Authorization" , authHeader );
148157    connection .setRequestProperty ("Content-Type" , contentType );
149158    connection .setRequestProperty ("Content-MD5" , contentMd5 );
159+     connection .setRequestProperty ("x-goog-hash" , "crc32c="  + crc32cBase64 );
150160    connection .setFixedLengthStreamingMode (xmlBodyBytes .length );
151161    connection .setDoOutput (true );
152162
0 commit comments