Skip to content

Commit a9fab09

Browse files
authored
feat: add Storage.BlobWriteOption.{meta,}generation{Not,}Match(long) methods to allow literal value construction (#1875)
#### New Methods * com.google.cloud.storage.Storage.BlobWriteOption.generationMatch(long) * com.google.cloud.storage.Storage.BlobWriteOption.generationNotMatch(long) * com.google.cloud.storage.Storage.BlobWriteOption.metagenerationMatch(long) * com.google.cloud.storage.Storage.BlobWriteOption.metagenerationNotMatch(long) Update UploadObject sample to use createFrom rather than trying to read the entire file into memory.
1 parent c74dace commit a9fab09

File tree

2 files changed

+40
-4
lines changed
  • google-cloud-storage/src/main/java/com/google/cloud/storage
  • samples/snippets/src/main/java/com/example/storage/object

2 files changed

+40
-4
lines changed

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

+36
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,15 @@ public static BlobWriteOption generationMatch() {
776776
return new BlobWriteOption(UnifiedOpts.generationMatchExtractor());
777777
}
778778

779+
/**
780+
* Returns an option for blob's data generation match. If this option is used the request will
781+
* fail if blob's generation does not match the provided value.
782+
*/
783+
@TransportCompatibility({Transport.HTTP, Transport.GRPC})
784+
public static BlobWriteOption generationMatch(long generation) {
785+
return new BlobWriteOption(UnifiedOpts.generationMatch(generation));
786+
}
787+
779788
/**
780789
* Returns an option for blob's data generation mismatch. If this option is used the request
781790
* will fail if generation matches.
@@ -785,6 +794,15 @@ public static BlobWriteOption generationNotMatch() {
785794
return new BlobWriteOption(UnifiedOpts.generationNotMatchExtractor());
786795
}
787796

797+
/**
798+
* Returns an option for blob's data generation mismatch. If this option is used the request
799+
* will fail if blob's generation does not match the provided value.
800+
*/
801+
@TransportCompatibility({Transport.HTTP, Transport.GRPC})
802+
public static BlobWriteOption generationNotMatch(long generation) {
803+
return new BlobWriteOption(UnifiedOpts.generationNotMatch(generation));
804+
}
805+
788806
/**
789807
* Returns an option for blob's metageneration match. If this option is used the request will
790808
* fail if metageneration does not match.
@@ -794,6 +812,15 @@ public static BlobWriteOption metagenerationMatch() {
794812
return new BlobWriteOption(UnifiedOpts.metagenerationMatchExtractor());
795813
}
796814

815+
/**
816+
* Returns an option for blob's metageneration match. If this option is used the request will
817+
* fail if blob's generation does not match the provided value.
818+
*/
819+
@TransportCompatibility({Transport.HTTP, Transport.GRPC})
820+
public static BlobWriteOption metagenerationMatch(long metageneration) {
821+
return new BlobWriteOption(UnifiedOpts.metagenerationMatch(metageneration));
822+
}
823+
797824
/**
798825
* Returns an option for blob's metageneration mismatch. If this option is used the request will
799826
* fail if metageneration matches.
@@ -803,6 +830,15 @@ public static BlobWriteOption metagenerationNotMatch() {
803830
return new BlobWriteOption(UnifiedOpts.metagenerationNotMatchExtractor());
804831
}
805832

833+
/**
834+
* Returns an option for blob's metageneration mismatch. If this option is used the request will
835+
* fail if blob's generation does not match the provided value.
836+
*/
837+
@TransportCompatibility({Transport.HTTP, Transport.GRPC})
838+
public static BlobWriteOption metagenerationNotMatch(long metageneration) {
839+
return new BlobWriteOption(UnifiedOpts.metagenerationNotMatch(metageneration));
840+
}
841+
806842
/**
807843
* Returns an option for blob's data MD5 hash match. If this option is used the request will
808844
* fail if blobs' data MD5 hash does not match.

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ public static void uploadObject(
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-
Storage.BlobTargetOption precondition;
51+
Storage.BlobWriteOption precondition;
5252
if (storage.get(bucketName, objectName) == null) {
5353
// For a target object that does not yet exist, set the DoesNotExist precondition.
5454
// This will cause the request to fail if the object is created before the request runs.
55-
precondition = Storage.BlobTargetOption.doesNotExist();
55+
precondition = Storage.BlobWriteOption.doesNotExist();
5656
} else {
5757
// If the destination already exists in your bucket, instead set a generation-match
5858
// precondition. This will cause the request to fail if the existing object's generation
5959
// changes before the request runs.
6060
precondition =
61-
Storage.BlobTargetOption.generationMatch(
61+
Storage.BlobWriteOption.generationMatch(
6262
storage.get(bucketName, objectName).getGeneration());
6363
}
64-
storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)), precondition);
64+
storage.createFrom(blobInfo, Paths.get(filePath), precondition);
6565

6666
System.out.println(
6767
"File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);

0 commit comments

Comments
 (0)