client-java: provide Content-MD5 when uploading chunks (#3761)

* client-java: provide Content-MD5 when uploading chunks

* Update SeaweedWrite.java
This commit is contained in:
kvtb 2022-09-30 05:56:12 +00:00 committed by GitHub
parent 56c94cc08e
commit 620be2be16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,7 +14,9 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.security.MessageDigest;
import java.util.List; import java.util.List;
import java.util.Base64;
public class SeaweedWrite { public class SeaweedWrite {
@ -123,13 +125,20 @@ public class SeaweedWrite {
final byte[] bytes, final byte[] bytes,
final long bytesOffset, final long bytesLength, final long bytesOffset, final long bytesLength,
byte[] cipherKey) throws IOException { byte[] cipherKey) throws IOException {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (java.security.NoSuchAlgorithmException e) {
}
InputStream inputStream = null; InputStream inputStream = null;
if (cipherKey == null || cipherKey.length == 0) { if (cipherKey == null || cipherKey.length == 0) {
md.update(bytes, (int) bytesOffset, (int) bytesLength);
inputStream = new ByteArrayInputStream(bytes, (int) bytesOffset, (int) bytesLength); inputStream = new ByteArrayInputStream(bytes, (int) bytesOffset, (int) bytesLength);
} else { } else {
try { try {
byte[] encryptedBytes = SeaweedCipher.encrypt(bytes, (int) bytesOffset, (int) bytesLength, cipherKey); byte[] encryptedBytes = SeaweedCipher.encrypt(bytes, (int) bytesOffset, (int) bytesLength, cipherKey);
md.update(encryptedBytes);
inputStream = new ByteArrayInputStream(encryptedBytes, 0, encryptedBytes.length); inputStream = new ByteArrayInputStream(encryptedBytes, 0, encryptedBytes.length);
} catch (Exception e) { } catch (Exception e) {
throw new IOException("fail to encrypt data", e); throw new IOException("fail to encrypt data", e);
@ -140,6 +149,7 @@ public class SeaweedWrite {
if (auth != null && auth.length() != 0) { if (auth != null && auth.length() != 0) {
post.addHeader("Authorization", "BEARER " + auth); post.addHeader("Authorization", "BEARER " + auth);
} }
post.addHeader("Content-MD5", Base64.getEncoder().encodeToString(md.digest()));
post.setEntity(MultipartEntityBuilder.create() post.setEntity(MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)