Skip to content

Commit

Permalink
feat: disableGzipContent option on create with InputStream (#36) (#82)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nblair authored Feb 14, 2020
1 parent 3809576 commit 65d3739
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,33 @@ public void testCreateBlobFromStream() throws IOException {
assertEquals(-1, byteStream.read(streamBytes));
}

@Test
public void testCreateBlobFromStreamDisableGzipContent() throws IOException {
Capture<ByteArrayInputStream> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 65d3739

Please sign in to comment.