Skip to content

Commit 96beca2

Browse files
fix: add missing preconditions and update samples (#1753)
1 parent 4662572 commit 96beca2

File tree

8 files changed

+132
-39
lines changed

8 files changed

+132
-39
lines changed

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

+36
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,24 @@ public static BlobTargetOption generationMatch() {
626626
return new BlobTargetOption(UnifiedOpts.generationMatchExtractor());
627627
}
628628

629+
/**
630+
* Returns an option for blob's data generation match. If this option is used the request will
631+
* fail if blob's generation does not match the provided value.
632+
*/
633+
@TransportCompatibility({Transport.HTTP, Transport.GRPC})
634+
public static BlobTargetOption generationMatch(long generation) {
635+
return new BlobTargetOption(UnifiedOpts.generationMatch(generation));
636+
}
637+
638+
/**
639+
* Returns an option for blob's data generation mismatch. If this option is used the request
640+
* will fail if generation matches the provided value.
641+
*/
642+
@TransportCompatibility({Transport.HTTP, Transport.GRPC})
643+
public static BlobTargetOption generationNotMatch(long generation) {
644+
return new BlobTargetOption(UnifiedOpts.generationNotMatch(generation));
645+
}
646+
629647
/**
630648
* Returns an option for blob's data generation mismatch. If this option is used the request
631649
* will fail if generation matches.
@@ -644,6 +662,24 @@ public static BlobTargetOption metagenerationMatch() {
644662
return new BlobTargetOption(UnifiedOpts.metagenerationMatchExtractor());
645663
}
646664

665+
/**
666+
* Returns an option for blob's metageneration match. If this option is used the request will
667+
* fail if blob's metageneration does not match the provided value.
668+
*/
669+
@TransportCompatibility({Transport.HTTP, Transport.GRPC})
670+
public static BlobTargetOption metagenerationMatch(long metageneration) {
671+
return new BlobTargetOption(UnifiedOpts.metagenerationMatch(metageneration));
672+
}
673+
674+
/**
675+
* Returns an option for blob's metageneration mismatch. If this option is used the request will
676+
* fail if metageneration matches the provided value.
677+
*/
678+
@TransportCompatibility({Transport.HTTP, Transport.GRPC})
679+
public static BlobTargetOption metagenerationNotMatch(long metageneration) {
680+
return new BlobTargetOption(UnifiedOpts.metagenerationNotMatch(metageneration));
681+
}
682+
647683
/**
648684
* Returns an option for blob's metageneration mismatch. If this option is used the request will
649685
* fail if metageneration matches.

samples/snippets/src/main/java/com/example/storage/object/ComposeObject.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ public static void composeObject(
4949
// Optional: set a generation-match precondition to avoid potential race
5050
// conditions and data corruptions. The request returns a 412 error if the
5151
// preconditions are not met.
52-
// For a target object that does not yet exist, set the DoesNotExist precondition.
53-
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
54-
// If the destination already exists in your bucket, instead set a generation-match
55-
// precondition:
56-
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
52+
Storage.BlobTargetOption precondition;
53+
if (storage.get(bucketName, targetObjectName) == null) {
54+
// For a target object that does not yet exist, set the DoesNotExist precondition.
55+
// This will cause the request to fail if the object is created before the request runs.
56+
precondition = Storage.BlobTargetOption.doesNotExist();
57+
} else {
58+
// If the destination already exists in your bucket, instead set a generation-match
59+
// precondition. This will cause the request to fail if the existing object's generation
60+
// changes before the request runs.
61+
precondition =
62+
Storage.BlobTargetOption.generationMatch(
63+
storage.get(bucketName, targetObjectName).getGeneration());
64+
}
5765

5866
Storage.ComposeRequest composeRequest =
5967
Storage.ComposeRequest.newBuilder()

samples/snippets/src/main/java/com/example/storage/object/CopyObject.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,20 @@ public static void copyObject(
4646
// Optional: set a generation-match precondition to avoid potential race
4747
// conditions and data corruptions. The request returns a 412 error if the
4848
// preconditions are not met.
49-
// For a target object that does not yet exist, set the DoesNotExist precondition.
50-
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
51-
// If the destination already exists in your bucket, instead set a generation-match
52-
// precondition:
53-
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
49+
Storage.BlobTargetOption precondition;
50+
if (storage.get(targetBucketName, objectName) == null) {
51+
// For a target object that does not yet exist, set the DoesNotExist precondition.
52+
// This will cause the request to fail if the object is created before the request runs.
53+
precondition = Storage.BlobTargetOption.doesNotExist();
54+
55+
} else {
56+
// If the destination already exists in your bucket, instead set a generation-match
57+
// precondition. This will cause the request to fail if the existing object's generation
58+
// changes before the request runs.
59+
precondition =
60+
Storage.BlobTargetOption.generationMatch(
61+
storage.get(targetBucketName, objectName).getGeneration());
62+
}
5463

5564
storage.copy(
5665
Storage.CopyRequest.newBuilder().setSource(source).setTarget(target, precondition).build());

samples/snippets/src/main/java/com/example/storage/object/CopyOldVersionOfObject.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,19 @@ public static void copyOldVersionOfObject(
4848
// Optional: set a generation-match precondition to avoid potential race
4949
// conditions and data corruptions. The request returns a 412 error if the
5050
// preconditions are not met.
51-
// For a target object that does not yet exist, set the DoesNotExist precondition.
52-
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
53-
// If the destination already exists in your bucket, instead set a generation-match
54-
// precondition:
55-
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
51+
Storage.BlobTargetOption precondition;
52+
if (storage.get(bucketName, newObjectName) == null) {
53+
// For a target object that does not yet exist, set the DoesNotExist precondition.
54+
// This will cause the request to fail if the object is created before the request runs.
55+
precondition = Storage.BlobTargetOption.doesNotExist();
56+
} else {
57+
// If the destination already exists in your bucket, instead set a generation-match
58+
// precondition. This will cause the request to fail if the existing object's generation
59+
// changes before the request runs.
60+
precondition =
61+
Storage.BlobTargetOption.generationMatch(
62+
storage.get(bucketName, newObjectName).getGeneration());
63+
}
5664

5765
Storage.CopyRequest copyRequest =
5866
Storage.CopyRequest.newBuilder()

samples/snippets/src/main/java/com/example/storage/object/MoveObject.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,19 @@ public static void moveObject(
5252
// Optional: set a generation-match precondition to avoid potential race
5353
// conditions and data corruptions. The request returns a 412 error if the
5454
// preconditions are not met.
55-
// For a target object that does not yet exist, set the DoesNotExist precondition.
56-
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
57-
// If the destination already exists in your bucket, instead set a generation-match
58-
// precondition:
59-
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
55+
Storage.BlobTargetOption precondition;
56+
if (storage.get(targetBucketName, targetObjectName) == null) {
57+
// For a target object that does not yet exist, set the DoesNotExist precondition.
58+
// This will cause the request to fail if the object is created before the request runs.
59+
precondition = Storage.BlobTargetOption.doesNotExist();
60+
} else {
61+
// If the destination already exists in your bucket, instead set a generation-match
62+
// precondition. This will cause the request to fail if the existing object's generation
63+
// changes before the request runs.
64+
precondition =
65+
Storage.BlobTargetOption.generationMatch(
66+
storage.get(targetBucketName, targetObjectName).getGeneration());
67+
}
6068

6169
// Copy source object to target object
6270
storage.copy(

samples/snippets/src/main/java/com/example/storage/object/UploadEncryptedObject.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,19 @@ public static void uploadEncryptedObject(
5252
// Optional: set a generation-match precondition to avoid potential race
5353
// conditions and data corruptions. The request returns a 412 error if the
5454
// preconditions are not met.
55-
// For a target object that does not yet exist, set the DoesNotExist precondition.
56-
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
57-
// If the destination already exists in your bucket, instead set a generation-match
58-
// precondition:
59-
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
55+
Storage.BlobTargetOption precondition;
56+
if (storage.get(bucketName, objectName) == null) {
57+
// For a target object that does not yet exist, set the DoesNotExist precondition.
58+
// This will cause the request to fail if the object is created before the request runs.
59+
precondition = Storage.BlobTargetOption.doesNotExist();
60+
} else {
61+
// If the destination already exists in your bucket, instead set a generation-match
62+
// precondition. This will cause the request to fail if the existing object's generation
63+
// changes before the request runs.
64+
precondition =
65+
Storage.BlobTargetOption.generationMatch(
66+
storage.get(bucketName, objectName).getGeneration());
67+
}
6068

6169
storage.create(
6270
blobInfo,

samples/snippets/src/main/java/com/example/storage/object/UploadKmsEncryptedObject.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ public static void uploadKmsEncryptedObject(
4949
// Optional: set a generation-match precondition to avoid potential race
5050
// conditions and data corruptions. The request returns a 412 error if the
5151
// preconditions are not met.
52-
// For a target object that does not yet exist, set the DoesNotExist precondition.
53-
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
54-
// If the destination already exists in your bucket, instead set a generation-match
55-
// precondition:
56-
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
52+
Storage.BlobTargetOption precondition;
53+
if (storage.get(bucketName, objectName) == null) {
54+
// For a target object that does not yet exist, set the DoesNotExist precondition.
55+
// This will cause the request to fail if the object is created before the request runs.
56+
precondition = Storage.BlobTargetOption.doesNotExist();
57+
} else {
58+
// If the destination already exists in your bucket, instead set a generation-match
59+
// precondition. This will cause the request to fail if the existing object's generation
60+
// changes before the request runs.
61+
precondition =
62+
Storage.BlobTargetOption.generationMatch(
63+
storage.get(bucketName, objectName).getGeneration());
64+
}
5765

5866
storage.create(blobInfo, data, Storage.BlobTargetOption.kmsKeyName(kmsKeyName), precondition);
5967

samples/snippets/src/main/java/com/example/storage/object/UploadObject.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,26 @@ public static void uploadObject(
4141
// The path to your file to upload
4242
// String filePath = "path/to/your/file"
4343

44-
// Optional: set a generation-match precondition to avoid potential race
45-
// conditions and data corruptions. The request returns a 412 error if the
46-
// preconditions are not met.
47-
// For a target object that does not yet exist, set the DoesNotExist precondition.
48-
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
49-
// If the destination already exists in your bucket, instead set a generation-match
50-
// precondition:
51-
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
52-
5344
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
5445
BlobId blobId = BlobId.of(bucketName, objectName);
5546
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
47+
48+
// Optional: set a generation-match precondition to avoid potential race
49+
// conditions and data corruptions. The request returns a 412 error if the
50+
// preconditions are not met.
51+
Storage.BlobTargetOption precondition;
52+
if (storage.get(bucketName, objectName) == null) {
53+
// For a target object that does not yet exist, set the DoesNotExist precondition.
54+
// This will cause the request to fail if the object is created before the request runs.
55+
precondition = Storage.BlobTargetOption.doesNotExist();
56+
} else {
57+
// If the destination already exists in your bucket, instead set a generation-match
58+
// precondition. This will cause the request to fail if the existing object's generation
59+
// changes before the request runs.
60+
precondition =
61+
Storage.BlobTargetOption.generationMatch(
62+
storage.get(bucketName, objectName).getGeneration());
63+
}
5664
storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)), precondition);
5765

5866
System.out.println(

0 commit comments

Comments
 (0)