Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing preconditions and update samples #1753

Merged
merged 9 commits into from
Dec 16, 2022
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ If you are using Maven without BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.1.3')
implementation platform('com.google.cloud:libraries-bom:26.1.4')

implementation 'com.google.cloud:google-cloud-storage'
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,15 @@ public static BlobTargetOption generationMatch() {
return new BlobTargetOption(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 BlobTargetOption generationMatch(long generation) {
return new BlobTargetOption(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 @@ -641,6 +650,15 @@ public static BlobTargetOption metagenerationMatch() {
return new BlobTargetOption(UnifiedOpts.metagenerationMatchExtractor());
}

/**
* Returns an option for blob's metageneration match. If this option is used the request will
* fail if blob's metageneration does not match the provided value.
*/
@TransportCompatibility({Transport.HTTP, Transport.GRPC})
public static BlobTargetOption metaGenerationMatch(long metageneration) {
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
return new BlobTargetOption(UnifiedOpts.generationMatch(metageneration));
}

/**
* Returns an option for blob's metageneration mismatch. If this option is used the request will
* fail if metageneration matches.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ public static void composeObject(
// conditions and data corruptions. The request returns a 412 error if the
// preconditions are not met.
// 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.
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
// If the destination already exists in your bucket, instead set a generation-match
// precondition:
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
// precondition. This will cause the request to fail if the existing object's generation
// changes before the request runs.
if (storage.get(bucketName, targetObjectName) != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one of these should be commented out as just an example rather than having the code overwrite the first precondition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes more sense, because it will actually behave appropriately based on whether the object already exists or not. If it doesn't exist, the original precondition isn't overwritten

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would look better as

Storage.BlobTargetOption precondition;
if (storage.get(bucketName, objectName != null) {
    precondition = ...;
} else {
    precondition = ...;

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative that avoids the duplicate storage.get call:

Storage.BlobTargetOption precondition = 
  Optional.ofNullable(storage.get(bucketName, targetObjectName))
    .map(Storage::getGeneration)
    .map(Storage.BlobTargetOption::generationMatch)
    .orElse(Storage.BlobTargetOption.doesNotExist())

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @JesseLovelace suggestion here is the most readable. If we wanted to avoid the duplicate call could we could save the object retrieved.

precondition =
Storage.BlobTargetOption.generationMatch(
storage.get(bucketName, targetObjectName).getGeneration());
}

Storage.ComposeRequest composeRequest =
Storage.ComposeRequest.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ public static void copyObject(
// conditions and data corruptions. The request returns a 412 error if the
// preconditions are not met.
// 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.
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
// If the destination already exists in your bucket, instead set a generation-match
// precondition:
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
// precondition. This will cause the request to fail if the existing object's generation
// changes before the request runs.
if (storage.get(targetBucketName, objectName) != null) {
precondition =
Storage.BlobTargetOption.generationMatch(
storage.get(targetBucketName, objectName).getGeneration());
}

storage.copy(
Storage.CopyRequest.newBuilder().setSource(source).setTarget(target, precondition).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,16 @@ public static void copyOldVersionOfObject(
// conditions and data corruptions. The request returns a 412 error if the
// preconditions are not met.
// 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.
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
// If the destination already exists in your bucket, instead set a generation-match
// precondition:
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
// precondition. This will cause the request to fail if the existing object's generation
// changes before the request runs.
if (storage.get(bucketName, newObjectName) != null) {
precondition =
Storage.BlobTargetOption.generationMatch(
storage.get(bucketName, newObjectName).getGeneration());
}

Storage.CopyRequest copyRequest =
Storage.CopyRequest.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ public static void moveObject(
// conditions and data corruptions. The request returns a 412 error if the
// preconditions are not met.
// 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.
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
// If the destination already exists in your bucket, instead set a generation-match
// precondition:
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
// precondition. This will cause the request to fail if the existing object's generation
// changes before the request runs.
if (storage.get(targetBucketName, targetObjectName) != null) {
precondition =
Storage.BlobTargetOption.generationMatch(
storage.get(targetBucketName, targetObjectName).getGeneration());
}

// Copy source object to target object
storage.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ public static void uploadEncryptedObject(
// conditions and data corruptions. The request returns a 412 error if the
// preconditions are not met.
// 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.
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
// If the destination already exists in your bucket, instead set a generation-match
// precondition:
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
// precondition. This will cause the request to fail if the existing object's generation
// changes before the request runs.
if (storage.get(bucketName, objectName) != null) {
precondition =
Storage.BlobTargetOption.generationMatch(
storage.get(bucketName, objectName).getGeneration());
}

storage.create(
blobInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ public static void uploadKmsEncryptedObject(
// conditions and data corruptions. The request returns a 412 error if the
// preconditions are not met.
// 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.
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
// If the destination already exists in your bucket, instead set a generation-match
// precondition:
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
// precondition. This will cause the request to fail if the existing object's generation
// changes before the request runs.
if (storage.get(bucketName, objectName) != null) {
precondition =
Storage.BlobTargetOption.generationMatch(
storage.get(bucketName, objectName).getGeneration());
}

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

Expand Down