Skip to content

Commit 64cf731

Browse files
committed
fix: ensure object generation is sent for Storage#update(BlobInfo) using HTTP Transport
If a generation is present in the provided BlobInfo ensure it is sent to GCS. This is a hard requirement when using versioned buckets and trying to update a non-current object version. Effects all version 2.42.0 through 2.50.0 Regression introduced in #2664 Fixes #2980
1 parent 704af65 commit 64cf731

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,11 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
564564
} else {
565565
StorageObject tmp = codecs.blobInfo().encode(updated);
566566
StorageObject pb = new StorageObject();
567-
Stream.concat(modifiedFields.stream(), BlobField.REQUIRED_FIELDS.stream())
567+
Stream.of(
568+
modifiedFields.stream(),
569+
BlobField.REQUIRED_FIELDS.stream(),
570+
Stream.of(BlobField.GENERATION))
571+
.flatMap(s -> s) // .flatten()
568572
.map(
569573
f -> {
570574
if (f instanceof NestedNamedField) {

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

+25
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
import java.util.stream.Stream;
9999
import java.util.stream.StreamSupport;
100100
import javax.crypto.spec.SecretKeySpec;
101+
import org.checkerframework.checker.nullness.qual.NonNull;
101102
import org.junit.Test;
102103
import org.junit.runner.RunWith;
103104

@@ -1582,4 +1583,28 @@ public void testUpdateBlob_noModification() {
15821583
Blob gen2 = storage.update(gen1);
15831584
assertThat(gen2).isEqualTo(gen1);
15841585
}
1586+
1587+
@Test
1588+
public void blob_update() throws Exception {
1589+
ImmutableMap<@NonNull String, @NonNull String> meta1 = ImmutableMap.of("k1", "v1");
1590+
ImmutableMap<@NonNull String, @NonNull String> meta2 = ImmutableMap.of("k1", "v2");
1591+
ImmutableMap<@NonNull String, @NonNull String> meta3 = ImmutableMap.of("k1", "v1", "k2", "n1");
1592+
1593+
String randomObjectName = generator.randomObjectName();
1594+
BlobInfo info1 =
1595+
BlobInfo.newBuilder(versionedBucket, randomObjectName).setMetadata(meta1).build();
1596+
BlobInfo info2 =
1597+
BlobInfo.newBuilder(versionedBucket, randomObjectName).setMetadata(meta2).build();
1598+
1599+
BlobInfo gen1 = storage.create(info1);
1600+
BlobInfo gen2 = storage.create(info2);
1601+
1602+
BlobInfo update1 = gen1.toBuilder().setMetadata(meta3).build();
1603+
1604+
BlobInfo gen1_2 = storage.update(update1);
1605+
1606+
assertAll(
1607+
() -> assertThat(gen1_2.getMetadata()).isEqualTo(meta3),
1608+
() -> assertThat(gen1_2.getGeneration()).isEqualTo(gen1.getGeneration()));
1609+
}
15851610
}

0 commit comments

Comments
 (0)