diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index bfc2cdb09..554611e98 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -776,6 +776,15 @@ public static BlobWriteOption generationMatch() { return new BlobWriteOption(UnifiedOpts.generationMatchExtractor()); } + /** + * Returns an option for blob's data generation match. If this option is used the request will + * fail if blob's generation does not match the provided value. + */ + @TransportCompatibility({Transport.HTTP, Transport.GRPC}) + public static BlobWriteOption generationMatch(long generation) { + return new BlobWriteOption(UnifiedOpts.generationMatch(generation)); + } + /** * Returns an option for blob's data generation mismatch. If this option is used the request * will fail if generation matches. @@ -785,6 +794,15 @@ public static BlobWriteOption generationNotMatch() { return new BlobWriteOption(UnifiedOpts.generationNotMatchExtractor()); } + /** + * Returns an option for blob's data generation mismatch. If this option is used the request + * will fail if blob's generation does not match the provided value. + */ + @TransportCompatibility({Transport.HTTP, Transport.GRPC}) + public static BlobWriteOption generationNotMatch(long generation) { + return new BlobWriteOption(UnifiedOpts.generationNotMatch(generation)); + } + /** * Returns an option for blob's metageneration match. If this option is used the request will * fail if metageneration does not match. @@ -794,6 +812,15 @@ public static BlobWriteOption metagenerationMatch() { return new BlobWriteOption(UnifiedOpts.metagenerationMatchExtractor()); } + /** + * Returns an option for blob's metageneration match. If this option is used the request will + * fail if blob's generation does not match the provided value. + */ + @TransportCompatibility({Transport.HTTP, Transport.GRPC}) + public static BlobWriteOption metagenerationMatch(long metageneration) { + return new BlobWriteOption(UnifiedOpts.metagenerationMatch(metageneration)); + } + /** * Returns an option for blob's metageneration mismatch. If this option is used the request will * fail if metageneration matches. @@ -803,6 +830,15 @@ public static BlobWriteOption metagenerationNotMatch() { return new BlobWriteOption(UnifiedOpts.metagenerationNotMatchExtractor()); } + /** + * Returns an option for blob's metageneration mismatch. If this option is used the request will + * fail if blob's generation does not match the provided value. + */ + @TransportCompatibility({Transport.HTTP, Transport.GRPC}) + public static BlobWriteOption metagenerationNotMatch(long metageneration) { + return new BlobWriteOption(UnifiedOpts.metagenerationNotMatch(metageneration)); + } + /** * Returns an option for blob's data MD5 hash match. If this option is used the request will * fail if blobs' data MD5 hash does not match. diff --git a/samples/snippets/src/main/java/com/example/storage/object/UploadObject.java b/samples/snippets/src/main/java/com/example/storage/object/UploadObject.java index 6472edeb8..d1f88007d 100644 --- a/samples/snippets/src/main/java/com/example/storage/object/UploadObject.java +++ b/samples/snippets/src/main/java/com/example/storage/object/UploadObject.java @@ -48,20 +48,20 @@ public static void uploadObject( // Optional: set a generation-match precondition to avoid potential race // conditions and data corruptions. The request returns a 412 error if the // preconditions are not met. - Storage.BlobTargetOption precondition; + Storage.BlobWriteOption precondition; if (storage.get(bucketName, objectName) == null) { // For a target object that does not yet exist, set the DoesNotExist precondition. // This will cause the request to fail if the object is created before the request runs. - precondition = Storage.BlobTargetOption.doesNotExist(); + precondition = Storage.BlobWriteOption.doesNotExist(); } else { // If the destination already exists in your bucket, instead set a generation-match // precondition. This will cause the request to fail if the existing object's generation // changes before the request runs. precondition = - Storage.BlobTargetOption.generationMatch( + Storage.BlobWriteOption.generationMatch( storage.get(bucketName, objectName).getGeneration()); } - storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)), precondition); + storage.createFrom(blobInfo, Paths.get(filePath), precondition); System.out.println( "File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);