Skip to content

Commit dc883cc

Browse files
authored
fix: update Storage#readAllBytes to respect shouldReturnRawInputStream option (#2635)
1 parent 758b3dd commit dc883cc

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

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
@@ -790,6 +790,9 @@ public byte[] load(StorageObject from, Map<Option, ?> options) {
790790
.setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options))
791791
.setUserProject(Option.USER_PROJECT.getString(options));
792792
setEncryptionHeaders(getRequest.getRequestHeaders(), ENCRYPTION_KEY_PREFIX, options);
793+
if (Option.RETURN_RAW_INPUT_STREAM.getBoolean(options) != null) {
794+
getRequest.setReturnRawInputStream(Option.RETURN_RAW_INPUT_STREAM.getBoolean(options));
795+
}
793796
ByteArrayOutputStream out = new ByteArrayOutputStream();
794797
getRequest.executeMedia().download(out);
795798
return out.toByteArray();
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.storage.it;
18+
19+
import static com.google.cloud.storage.TestUtils.xxd;
20+
import static com.google.common.truth.Truth.assertThat;
21+
22+
import com.google.cloud.storage.Blob;
23+
import com.google.cloud.storage.BlobInfo;
24+
import com.google.cloud.storage.BucketInfo;
25+
import com.google.cloud.storage.Storage;
26+
import com.google.cloud.storage.Storage.BlobSourceOption;
27+
import com.google.cloud.storage.Storage.BlobTargetOption;
28+
import com.google.cloud.storage.TestUtils;
29+
import com.google.cloud.storage.TransportCompatibility.Transport;
30+
import com.google.cloud.storage.it.runner.StorageITRunner;
31+
import com.google.cloud.storage.it.runner.annotations.Backend;
32+
import com.google.cloud.storage.it.runner.annotations.CrossRun;
33+
import com.google.cloud.storage.it.runner.annotations.Inject;
34+
import com.google.cloud.storage.it.runner.registry.Generator;
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
38+
@RunWith(StorageITRunner.class)
39+
@CrossRun(
40+
backends = {Backend.PROD},
41+
transports = {Transport.HTTP, Transport.GRPC})
42+
public final class ITGzipTest {
43+
44+
private static final ChecksummedTestContent CHECKSUMMED_TEST_CONTENT =
45+
ChecksummedTestContent.of("Hello, to the world!!!");
46+
private static final byte[] GZIPPED_CONTENT =
47+
TestUtils.gzipBytes(CHECKSUMMED_TEST_CONTENT.getBytes());
48+
49+
@Inject public Storage storage;
50+
@Inject public BucketInfo bucket;
51+
@Inject public Generator generator;
52+
53+
@Test
54+
public void noMetadata_noOption() {
55+
String name = generator.randomObjectName();
56+
BlobInfo info = BlobInfo.newBuilder(bucket, name).build();
57+
Blob gen1 = storage.create(info, CHECKSUMMED_TEST_CONTENT.getBytes());
58+
assertThat(gen1.getContentEncoding()).isAnyOf(null, ""); // json null, grpc ""
59+
byte[] actual =
60+
storage.readAllBytes(gen1.getBlobId(), BlobSourceOption.shouldReturnRawInputStream(true));
61+
assertThat(xxd(actual)).isEqualTo(xxd(CHECKSUMMED_TEST_CONTENT.getBytes()));
62+
}
63+
64+
@Test
65+
public void yesMetadata_noOption() {
66+
String name = generator.randomObjectName();
67+
BlobInfo info = BlobInfo.newBuilder(bucket, name).setContentEncoding("gzip").build();
68+
Blob gen1 = storage.create(info, GZIPPED_CONTENT);
69+
assertThat(gen1.getContentEncoding()).isEqualTo("gzip");
70+
byte[] actual =
71+
storage.readAllBytes(gen1.getBlobId(), BlobSourceOption.shouldReturnRawInputStream(true));
72+
assertThat(xxd(actual)).isEqualTo(xxd(GZIPPED_CONTENT));
73+
}
74+
75+
@Test
76+
public void noMetadata_yesOption() {
77+
String name = generator.randomObjectName();
78+
BlobInfo info = BlobInfo.newBuilder(bucket, name).build();
79+
Blob gen1 =
80+
storage.create(
81+
info, CHECKSUMMED_TEST_CONTENT.getBytes(), BlobTargetOption.disableGzipContent());
82+
assertThat(gen1.getContentEncoding()).isAnyOf(null, ""); // json null, grpc ""
83+
byte[] actual =
84+
storage.readAllBytes(gen1.getBlobId(), BlobSourceOption.shouldReturnRawInputStream(true));
85+
assertThat(xxd(actual)).isEqualTo(xxd(CHECKSUMMED_TEST_CONTENT.getBytes()));
86+
}
87+
88+
@Test
89+
public void yesMetadata_yesOption() {
90+
String name = generator.randomObjectName();
91+
BlobInfo info = BlobInfo.newBuilder(bucket, name).setContentEncoding("gzip").build();
92+
Blob gen1 = storage.create(info, GZIPPED_CONTENT, BlobTargetOption.disableGzipContent());
93+
assertThat(gen1.getContentEncoding()).isEqualTo("gzip");
94+
byte[] actual =
95+
storage.readAllBytes(gen1.getBlobId(), BlobSourceOption.shouldReturnRawInputStream(true));
96+
assertThat(xxd(actual)).isEqualTo(xxd(GZIPPED_CONTENT));
97+
98+
byte[] actualUncompressed =
99+
storage.readAllBytes(gen1.getBlobId(), BlobSourceOption.shouldReturnRawInputStream(false));
100+
assertThat(xxd(actualUncompressed)).isEqualTo(xxd(CHECKSUMMED_TEST_CONTENT.getBytes()));
101+
}
102+
}

0 commit comments

Comments
 (0)