Skip to content

Commit 8c59c64

Browse files
authored
fix: update GrpcStorageImpl#get(BlobId) to return null on 404 (#1772)
Update GrpcStorageImpl#get(BlobId) to match the behavior of StorageImpl#get(BlobId) and return a null values when 404 is encountered.
1 parent 2770a38 commit 8c59c64

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,13 @@ public Blob get(BlobId blob, BlobGetOption... options) {
417417
return Retrying.run(
418418
getOptions(),
419419
retryAlgorithmManager.getFor(req),
420-
() -> storageClient.getObjectCallable().call(req, grpcCallContext),
420+
() -> {
421+
try {
422+
return storageClient.getObjectCallable().call(req, grpcCallContext);
423+
} catch (NotFoundException ignore) {
424+
return null;
425+
}
426+
},
421427
syntaxDecoders.blob.andThen(opts.clearBlobFields()));
422428
}
423429

google-cloud-storage/src/test/java/com/google/cloud/storage/PackagePrivateMethodWorkarounds.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.api.services.storage.model.StorageObject;
2020
import com.google.cloud.WriteChannel;
2121
import com.google.cloud.storage.BucketInfo.BuilderImpl;
22+
import com.google.common.collect.ImmutableList;
2223
import java.util.Optional;
2324
import java.util.function.Consumer;
2425
import java.util.function.Function;
@@ -73,4 +74,8 @@ public static <T> void ifNonNull(@Nullable T t, Consumer<T> c) {
7374
public static <T1, T2> void ifNonNull(@Nullable T1 t, Function<T1, T2> map, Consumer<T2> c) {
7475
Utils.ifNonNull(t, map, c);
7576
}
77+
78+
public static BlobInfo noAcl(BlobInfo bi) {
79+
return bi.toBuilder().setOwner(null).setAcl(ImmutableList.of()).build();
80+
}
7681
}

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITObjectTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.google.cloud.storage.BucketFixture;
4141
import com.google.cloud.storage.BucketInfo;
4242
import com.google.cloud.storage.CopyWriter;
43+
import com.google.cloud.storage.PackagePrivateMethodWorkarounds;
4344
import com.google.cloud.storage.Storage;
4445
import com.google.cloud.storage.Storage.BlobField;
4546
import com.google.cloud.storage.Storage.BlobWriteOption;
@@ -488,6 +489,14 @@ public void testListBlobsEmptySelectedFields() throws InterruptedException {
488489
}
489490
}
490491

492+
@Test
493+
public void getBlobReturnNullOn404() {
494+
String bucketName = bucketFixture.getBucketInfo().getName();
495+
BlobId id = BlobId.of(bucketName, "__d_o_e_s__n_o_t__e_x_i_s_t__");
496+
Blob blob = storage.get(id);
497+
assertThat(blob).isNull();
498+
}
499+
491500
@Test(timeout = 7500)
492501
public void testListBlobRequesterPays() throws InterruptedException {
493502
unsetRequesterPays(storage, requesterPaysBucketFixture);
@@ -1435,25 +1444,21 @@ public void testAttemptDeletionObjectTemporaryHold() {
14351444

14361445
@Test
14371446
public void testBlobReload() throws Exception {
1438-
// GRPC Error Handling bug
1439-
// b/247621346
1440-
assumeTrue(clientName.startsWith("JSON"));
1441-
14421447
String blobName = "test-blob-reload";
14431448
BlobId blobId = BlobId.of(bucketFixture.getBucketInfo().getName(), blobName);
14441449
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
14451450
Blob blob = storage.create(blobInfo, new byte[] {0, 1, 2});
14461451

14471452
Blob blobUnchanged = blob.reload();
1448-
assertEquals(blob, blobUnchanged);
1453+
// gRPC and json have differing defaults on projections b/258835631
1454+
assertThat(blobUnchanged).isAnyOf(blob, PackagePrivateMethodWorkarounds.noAcl(blob));
14491455

14501456
blob.writer().close();
14511457
try {
14521458
blob.reload(Blob.BlobSourceOption.generationMatch());
14531459
fail("StorageException was expected");
14541460
} catch (StorageException e) {
14551461
assertEquals(412, e.getCode());
1456-
assertEquals("conditionNotMet", e.getReason());
14571462
}
14581463

14591464
Blob updated = blob.reload();

0 commit comments

Comments
 (0)