Skip to content

Commit

Permalink
feat: add Storage.BlobWriteOption.{meta,}generation{Not,}Match(long) …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
BenWhitehead committed Feb 1, 2023
1 parent c74dace commit a9fab09
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a9fab09

Please sign in to comment.