From 65d3739567427e49ca4abfd39702fd4022ee8e3c Mon Sep 17 00:00:00 2001 From: Nicholas Blair Date: Fri, 14 Feb 2020 17:23:59 -0600 Subject: [PATCH] feat: disableGzipContent option on create with InputStream (#36) (#82) 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 c7514b75c..527848747 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 @@ -582,7 +582,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()); @@ -714,6 +715,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 d13cc74a8..5c809c7b6 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 @@ -738,6 +738,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 e1788e6b6..7aad8a740 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";