googleapis/java-storage

Checksum verification when uploading file to cloud storage not working - version 2.36.1

Closed this issue · 2 comments

Hi,
I am trying to set md5 verification when I upload file as follows:
BlobId blobId = BlobId.of(bucketName, filePath);
BlobInfo.Builder blobInfoBuilder = BlobInfo.newBuilder(blobId);
blobInfoBuilder .setMd5FromHexString(md5Hex);
BlobInfo blobInfo = blobInfoBuilder.build();
WriteChannel writeChannel = getWriteChannel(blobInfo);
writeChannel.setChunkSize(gcsChunkSize);
OutputStream gcsOutputStream = Channels.newOutputStream(writeChannel);
gcsOutputStream .write();
gcsOutputStream.close();

Any md5 hex I am giving doesn't produce an error.
Does the server verify this MD5?
Thanks,
Itay

Hi @ihudedi from the looks of it you need to specify a BlobWriteOption when retrieving the write channel. See the docs here.

Additionally, we recommend using CRC32C as opposed to MD5.

What @ddelgrosso1 says is correct. The option must be passed when calling Storage#writer.

For a full code sample, take a look at our integration tests that verify checksum validation works via positive and negative cases:

try (ReadableByteChannel src = Channels.newChannel(new ByteArrayInputStream(bytes));
WriteChannel dst =
storage.writer(
blobInfo, BlobWriteOption.doesNotExist(), BlobWriteOption.crc32cMatch())) {
ByteStreams.copy(src, dst);
}