Skip to content

Commit 183d0c6

Browse files
committed
feat: disableGzipContent option on create with InputStream (#36)
Previously, only the methods to create blobs that take a byte[] argument offer the option to disable gzip compression; the methods that accept an InputStream argument do not. This is due to the BlobWriteOption enum missing a matching constant for BlobTargetOption.IF_DISABLE_GZIP_CONTENT. This change set adds a matching IF_DISABLE_GZIP_CONTENT constant to BlobWriteOption including the correct translation to StorageRpc.Option. The net result is that the Storage create functions that accept an InputStream now offer the option to disable gzip compression.
1 parent b93106b commit 183d0c6

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ enum Option {
567567
IF_CRC32C_MATCH,
568568
CUSTOMER_SUPPLIED_KEY,
569569
KMS_KEY_NAME,
570-
USER_PROJECT;
570+
USER_PROJECT,
571+
IF_DISABLE_GZIP_CONTENT;
571572

572573
StorageRpc.Option toRpcOption() {
573574
return StorageRpc.Option.valueOf(this.name());
@@ -699,6 +700,14 @@ public static BlobWriteOption kmsKeyName(String kmsKeyName) {
699700
public static BlobWriteOption userProject(String userProject) {
700701
return new BlobWriteOption(Option.USER_PROJECT, userProject);
701702
}
703+
704+
/**
705+
* Returns an option that signals automatic gzip compression should not be performed en route to
706+
* the bucket.
707+
*/
708+
public static BlobWriteOption disableGzipContent() {
709+
return new BlobWriteOption(Option.IF_DISABLE_GZIP_CONTENT, true);
710+
}
702711
}
703712

704713
/** Class for specifying blob source options. */

google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,33 @@ public void testCreateBlobFromStream() throws IOException {
741741
assertEquals(-1, byteStream.read(streamBytes));
742742
}
743743

744+
@Test
745+
public void testCreateBlobFromStreamDisableGzipContent() throws IOException {
746+
Capture<ByteArrayInputStream> capturedStream = Capture.newInstance();
747+
748+
ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT);
749+
BlobInfo.Builder infoBuilder = BLOB_INFO1.toBuilder();
750+
BlobInfo infoWithHashes = infoBuilder.setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build();
751+
BlobInfo infoWithoutHashes = infoBuilder.setMd5(null).setCrc32c(null).build();
752+
EasyMock.expect(
753+
storageRpcMock.create(
754+
EasyMock.eq(infoWithoutHashes.toPb()),
755+
EasyMock.capture(capturedStream),
756+
EasyMock.eq(BLOB_TARGET_OPTIONS_CREATE_DISABLE_GZIP_CONTENT)))
757+
.andReturn(BLOB_INFO1.toPb());
758+
EasyMock.replay(storageRpcMock);
759+
initializeService();
760+
761+
Blob blob = storage.create(infoWithHashes, fileStream, BlobWriteOption.disableGzipContent());
762+
763+
assertEquals(expectedBlob1, blob);
764+
ByteArrayInputStream byteStream = capturedStream.getValue();
765+
byte[] streamBytes = new byte[BLOB_CONTENT.length];
766+
assertEquals(BLOB_CONTENT.length, byteStream.read(streamBytes));
767+
assertArrayEquals(BLOB_CONTENT, streamBytes);
768+
assertEquals(-1, byteStream.read(streamBytes));
769+
}
770+
744771
@Test
745772
public void testCreateBlobFromStreamWithEncryptionKey() throws IOException {
746773
ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT);

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java

Lines changed: 15 additions & 0 deletions
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)