File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,8 @@ enum Option {
582582
IF_CRC32C_MATCH,
583583
CUSTOMER_SUPPLIED_KEY,
584584
KMS_KEY_NAME,
585-
USER_PROJECT;
585+
USER_PROJECT,
586+
IF_DISABLE_GZIP_CONTENT;
586587

587588
StorageRpc.Option toRpcOption() {
588589
return StorageRpc.Option.valueOf(this.name());
@@ -714,6 +715,14 @@ public static BlobWriteOption kmsKeyName(String kmsKeyName) {
714715
public static BlobWriteOption userProject(String userProject) {
715716
return new BlobWriteOption(Option.USER_PROJECT, userProject);
716717
}
718+
719+
/**
720+
* Returns an option that signals automatic gzip compression should not be performed en route to
721+
* the bucket.
722+
*/
723+
public static BlobWriteOption disableGzipContent() {
724+
return new BlobWriteOption(Option.IF_DISABLE_GZIP_CONTENT, true);
725+
}
717726
}
718727

719728
/** Class for specifying blob source options. */
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,33 @@ public void testCreateBlobFromStream() throws IOException {
738738
assertEquals(-1, byteStream.read(streamBytes));
739739
}
740740

741+
@Test
742+
public void testCreateBlobFromStreamDisableGzipContent() throws IOException {
743+
Capture<ByteArrayInputStream> capturedStream = Capture.newInstance();
744+
745+
ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT);
746+
BlobInfo.Builder infoBuilder = BLOB_INFO1.toBuilder();
747+
BlobInfo infoWithHashes = infoBuilder.setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build();
748+
BlobInfo infoWithoutHashes = infoBuilder.setMd5(null).setCrc32c(null).build();
749+
EasyMock.expect(
750+
storageRpcMock.create(
751+
EasyMock.eq(infoWithoutHashes.toPb()),
752+
EasyMock.capture(capturedStream),
753+
EasyMock.eq(BLOB_TARGET_OPTIONS_CREATE_DISABLE_GZIP_CONTENT)))
754+
.andReturn(BLOB_INFO1.toPb());
755+
EasyMock.replay(storageRpcMock);
756+
initializeService();
757+
758+
Blob blob = storage.create(infoWithHashes, fileStream, BlobWriteOption.disableGzipContent());
759+
760+
assertEquals(expectedBlob1, blob);
761+
ByteArrayInputStream byteStream = capturedStream.getValue();
762+
byte[] streamBytes = new byte[BLOB_CONTENT.length];
763+
assertEquals(BLOB_CONTENT.length, byteStream.read(streamBytes));
764+
assertArrayEquals(BLOB_CONTENT, streamBytes);
765+
assertEquals(-1, byteStream.read(streamBytes));
766+
}
767+
741768
@Test
742769
public void testCreateBlobFromStreamWithEncryptionKey() throws IOException {
743770
ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT);
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import com.google.cloud.storage.ServiceAccount;
7070
import com.google.cloud.storage.Storage;
7171
import com.google.cloud.storage.Storage.BlobField;
72+
import com.google.cloud.storage.Storage.BlobWriteOption;
7273
import com.google.cloud.storage.Storage.BucketField;
7374
import com.google.cloud.storage.StorageBatch;
7475
import com.google.cloud.storage.StorageBatchResult;
@@ -597,6 +598,20 @@ public void testCreateBlobStream() {
597598
assertEquals(BLOB_STRING_CONTENT, new String(readBytes, UTF_8));
598599
}
599600

601+
@Test
602+
public void testCreateBlobStreamDisableGzipContent() {
603+
String blobName = "test-create-blob-stream-disable-gzip-compression";
604+
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build();
605+
ByteArrayInputStream stream = new ByteArrayInputStream(BLOB_STRING_CONTENT.getBytes(UTF_8));
606+
Blob remoteBlob = storage.create(blob, stream, BlobWriteOption.disableGzipContent());
607+
assertNotNull(remoteBlob);
608+
assertEquals(blob.getBucket(), remoteBlob.getBucket());
609+
assertEquals(blob.getName(), remoteBlob.getName());
610+
assertEquals(blob.getContentType(), remoteBlob.getContentType());
611+
byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
612+
assertEquals(BLOB_STRING_CONTENT, new String(readBytes, UTF_8));
613+
}
614+
600615
@Test
601616
public void testCreateBlobFail() {
602617
String blobName = "test-create-blob-fail";

0 commit comments

Comments
 (0)