From 183d0c62b13c5984924cc34f237f926a299cf5e8 Mon Sep 17 00:00:00 2001 From: Nicholas Blair Date: Thu, 23 Jan 2020 11:19:05 -0600 Subject: [PATCH] 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. --- .../com/google/cloud/storage/Storage.java | 11 +++++++- .../google/cloud/storage/StorageImplTest.java | 27 +++++++++++++++++++ .../cloud/storage/it/ITStorageTest.java | 15 +++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index 77c88e97c6..db96fe0381 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -567,7 +567,8 @@ enum Option { IF_CRC32C_MATCH, CUSTOMER_SUPPLIED_KEY, KMS_KEY_NAME, - USER_PROJECT; + USER_PROJECT, + IF_DISABLE_GZIP_CONTENT; StorageRpc.Option toRpcOption() { return StorageRpc.Option.valueOf(this.name()); @@ -699,6 +700,14 @@ public static BlobWriteOption kmsKeyName(String kmsKeyName) { public static BlobWriteOption userProject(String userProject) { return new BlobWriteOption(Option.USER_PROJECT, userProject); } + + /** + * Returns an option that signals automatic gzip compression should not be performed en route to + * the bucket. + */ + public static BlobWriteOption disableGzipContent() { + return new BlobWriteOption(Option.IF_DISABLE_GZIP_CONTENT, true); + } } /** Class for specifying blob source options. */ diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java index 6e698123c4..1f55217843 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java @@ -741,6 +741,33 @@ public void testCreateBlobFromStream() throws IOException { assertEquals(-1, byteStream.read(streamBytes)); } + @Test + public void testCreateBlobFromStreamDisableGzipContent() throws IOException { + Capture capturedStream = Capture.newInstance(); + + ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT); + BlobInfo.Builder infoBuilder = BLOB_INFO1.toBuilder(); + BlobInfo infoWithHashes = infoBuilder.setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build(); + BlobInfo infoWithoutHashes = infoBuilder.setMd5(null).setCrc32c(null).build(); + EasyMock.expect( + storageRpcMock.create( + EasyMock.eq(infoWithoutHashes.toPb()), + EasyMock.capture(capturedStream), + EasyMock.eq(BLOB_TARGET_OPTIONS_CREATE_DISABLE_GZIP_CONTENT))) + .andReturn(BLOB_INFO1.toPb()); + EasyMock.replay(storageRpcMock); + initializeService(); + + Blob blob = storage.create(infoWithHashes, fileStream, BlobWriteOption.disableGzipContent()); + + assertEquals(expectedBlob1, blob); + ByteArrayInputStream byteStream = capturedStream.getValue(); + byte[] streamBytes = new byte[BLOB_CONTENT.length]; + assertEquals(BLOB_CONTENT.length, byteStream.read(streamBytes)); + assertArrayEquals(BLOB_CONTENT, streamBytes); + assertEquals(-1, byteStream.read(streamBytes)); + } + @Test public void testCreateBlobFromStreamWithEncryptionKey() throws IOException { ByteArrayInputStream fileStream = new ByteArrayInputStream(BLOB_CONTENT); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index e1788e6b6e..7aad8a7405 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -69,6 +69,7 @@ import com.google.cloud.storage.ServiceAccount; import com.google.cloud.storage.Storage; import com.google.cloud.storage.Storage.BlobField; +import com.google.cloud.storage.Storage.BlobWriteOption; import com.google.cloud.storage.Storage.BucketField; import com.google.cloud.storage.StorageBatch; import com.google.cloud.storage.StorageBatchResult; @@ -597,6 +598,20 @@ public void testCreateBlobStream() { assertEquals(BLOB_STRING_CONTENT, new String(readBytes, UTF_8)); } + @Test + public void testCreateBlobStreamDisableGzipContent() { + String blobName = "test-create-blob-stream-disable-gzip-compression"; + BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build(); + ByteArrayInputStream stream = new ByteArrayInputStream(BLOB_STRING_CONTENT.getBytes(UTF_8)); + Blob remoteBlob = storage.create(blob, stream, BlobWriteOption.disableGzipContent()); + assertNotNull(remoteBlob); + assertEquals(blob.getBucket(), remoteBlob.getBucket()); + assertEquals(blob.getName(), remoteBlob.getName()); + assertEquals(blob.getContentType(), remoteBlob.getContentType()); + byte[] readBytes = storage.readAllBytes(BUCKET, blobName); + assertEquals(BLOB_STRING_CONTENT, new String(readBytes, UTF_8)); + } + @Test public void testCreateBlobFail() { String blobName = "test-create-blob-fail";