From 06ef73ab4a5c1e1ea2b681a223134840867950ae Mon Sep 17 00:00:00 2001 From: Nicholas Blair Date: Sat, 14 Dec 2019 20:59:14 -0600 Subject: [PATCH 1/2] Add disableGzipContent option for create from InputStream 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 | 7 ++++- .../google/cloud/storage/StorageImplTest.java | 27 +++++++++++++++++++ .../cloud/storage/it/ITStorageTest.java | 15 +++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index 3091e0f690c5..904c640cc875 100644 --- a/google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-clients/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,10 @@ public static BlobWriteOption kmsKeyName(String kmsKeyName) { public static BlobWriteOption userProject(String userProject) { return new BlobWriteOption(Option.USER_PROJECT, userProject); } + + public static BlobWriteOption disableGzipContent() { + return new BlobWriteOption(Option.IF_DISABLE_GZIP_CONTENT, true); + } } /** Class for specifying blob source options. */ diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java index db003a92a7c6..1a0c79f5aa65 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java @@ -705,6 +705,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-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index 829839cb6b6d..adf7f54432a0 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-clients/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"; From 98f833253b1e5b35162aca3224e680a5aec8c53a Mon Sep 17 00:00:00 2001 From: Nicholas Blair Date: Thu, 19 Dec 2019 08:57:34 -0600 Subject: [PATCH 2/2] correct formatting --- .../java/com/google/cloud/storage/StorageImplTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java index 1a0c79f5aa65..4973a6b3d075 100644 --- a/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java +++ b/google-cloud-clients/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java @@ -714,10 +714,10 @@ public void testCreateBlobFromStreamDisableGzipContent() throws IOException { 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))) + 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();