Skip to content

Commit 97b1efc

Browse files
andrey-qlogicchingor13
authored andcommitted
Storage: Give users the ability to disable gzip content encoding to increase throughput (#4170)
* 3822: Added BlobTargetOption for user to set disabledGzipContent true * 3822: Changed method comment * 3822: Changed method comment with more explanation * 3822: Fixed comments after review. Added unit test * 3822: Fixed formatting errors in Storage class * 3822: Fixed formatting errors in StorageImplTest class
1 parent 940999d commit 97b1efc

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@ public static BlobTargetOption metagenerationNotMatch() {
346346
return new BlobTargetOption(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH);
347347
}
348348

349+
/**
350+
* Returns an option for blob's data disabledGzipContent. If this option is used, the request
351+
* will create a blob with disableGzipContent; at present, this is only for upload.
352+
*/
353+
public static BlobTargetOption disableGzipContent() {
354+
return new BlobTargetOption(StorageRpc.Option.IF_DISABLE_GZIP_CONTENT, true);
355+
}
356+
349357
/**
350358
* Returns an option to set a customer-supplied AES256 key for server-side encryption of the
351359
* blob.

google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ public StorageObject create(
287287
storageObject,
288288
new InputStreamContent(storageObject.getContentType(), content));
289289
insert.getMediaHttpUploader().setDirectUploadEnabled(true);
290+
Boolean disableGzipContent = Option.IF_DISABLE_GZIP_CONTENT.getBoolean(options);
291+
if (disableGzipContent != null)
292+
insert.getMediaHttpUploader().setDisableGZipContent(disableGzipContent);
290293
setEncryptionHeaders(insert.getRequestHeaders(), ENCRYPTION_KEY_PREFIX, options);
291294
return insert
292295
.setProjection(DEFAULT_PROJECTION)

google-cloud-clients/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ enum Option {
4848
IF_SOURCE_METAGENERATION_NOT_MATCH("ifSourceMetagenerationNotMatch"),
4949
IF_SOURCE_GENERATION_MATCH("ifSourceGenerationMatch"),
5050
IF_SOURCE_GENERATION_NOT_MATCH("ifSourceGenerationNotMatch"),
51+
IF_DISABLE_GZIP_CONTENT("disableGzipContent"),
5152
PREFIX("prefix"),
5253
MAX_RESULTS("maxResults"),
5354
PAGE_TOKEN("pageToken"),

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ public class StorageImplTest {
154154
private static final BlobTargetOption BLOB_TARGET_GENERATION = BlobTargetOption.generationMatch();
155155
private static final BlobTargetOption BLOB_TARGET_METAGENERATION =
156156
BlobTargetOption.metagenerationMatch();
157+
private static final BlobTargetOption BLOB_TARGET_DISABLE_GZIP_CONTENT =
158+
BlobTargetOption.disableGzipContent();
157159
private static final BlobTargetOption BLOB_TARGET_NOT_EXIST = BlobTargetOption.doesNotExist();
158160
private static final BlobTargetOption BLOB_TARGET_PREDEFINED_ACL =
159161
BlobTargetOption.predefinedAcl(Storage.PredefinedAcl.PRIVATE);
@@ -162,6 +164,8 @@ public class StorageImplTest {
162164
StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_INFO1.getMetageneration(),
163165
StorageRpc.Option.IF_GENERATION_MATCH, 0L,
164166
StorageRpc.Option.PREDEFINED_ACL, BUCKET_TARGET_PREDEFINED_ACL.getValue());
167+
private static final Map<StorageRpc.Option, ?> BLOB_TARGET_OPTIONS_CREATE_DISABLE_GZIP_CONTENT =
168+
ImmutableMap.of(StorageRpc.Option.IF_DISABLE_GZIP_CONTENT, true);
165169
private static final Map<StorageRpc.Option, ?> BLOB_TARGET_OPTIONS_UPDATE =
166170
ImmutableMap.of(
167171
StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_INFO1.getMetageneration(),
@@ -545,6 +549,32 @@ public void testCreateBlobWithOptions() throws IOException {
545549
assertEquals(-1, byteStream.read(streamBytes));
546550
}
547551

552+
@Test
553+
public void testCreateBlobWithDisabledGzipContent() throws IOException {
554+
Capture<ByteArrayInputStream> capturedStream = Capture.newInstance();
555+
EasyMock.expect(
556+
storageRpcMock.create(
557+
EasyMock.eq(
558+
BLOB_INFO1
559+
.toBuilder()
560+
.setMd5(CONTENT_MD5)
561+
.setCrc32c(CONTENT_CRC32C)
562+
.build()
563+
.toPb()),
564+
EasyMock.capture(capturedStream),
565+
EasyMock.eq(BLOB_TARGET_OPTIONS_CREATE_DISABLE_GZIP_CONTENT)))
566+
.andReturn(BLOB_INFO1.toPb());
567+
EasyMock.replay(storageRpcMock);
568+
initializeService();
569+
Blob blob = storage.create(BLOB_INFO1, BLOB_CONTENT, BLOB_TARGET_DISABLE_GZIP_CONTENT);
570+
assertEquals(expectedBlob1, blob);
571+
ByteArrayInputStream byteStream = capturedStream.getValue();
572+
byte[] streamBytes = new byte[BLOB_CONTENT.length];
573+
assertEquals(BLOB_CONTENT.length, byteStream.read(streamBytes));
574+
assertArrayEquals(BLOB_CONTENT, streamBytes);
575+
assertEquals(-1, byteStream.read(streamBytes));
576+
}
577+
548578
@Test
549579
public void testCreateBlobWithEncryptionKey() throws IOException {
550580
Capture<ByteArrayInputStream> capturedStream = Capture.newInstance();

0 commit comments

Comments
 (0)