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

docs: Add preconditions to some samples #1600

Merged
merged 7 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0')
implementation platform('com.google.cloud:libraries-bom:26.1.1')

implementation 'com.google.cloud:google-cloud-storage'
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,17 @@ public static void changeObjectFromCsekToKms(

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);

// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload returns a 412 error if
// the object's generation number does not match your precondition.
Storage.BlobSourceOption precondition =
Storage.BlobSourceOption.generationMatch(storage.get(blobId).getGeneration());
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could 404 and return null if there isn't an object for blobId. Possibly move it to it's own line to ease debugging?


Storage.CopyRequest request =
Storage.CopyRequest.newBuilder()
.setSource(blobId)
.setSourceOptions(Storage.BlobSourceOption.decryptionKey(decryptionKey))
.setSourceOptions(Storage.BlobSourceOption.decryptionKey(decryptionKey), precondition)
.setTarget(blobId, Storage.BlobTargetOption.kmsKeyName(kmsKeyName))
.build();
storage.copy(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,21 @@ public static void changeObjectStorageClass(
StorageClass storageClass = StorageClass.COLDLINE;

// You can't change an object's storage class directly, the only way is to rewrite the object
// with the
// desired storage class
// with the desired storage class

BlobInfo targetBlob = BlobInfo.newBuilder(blobId).setStorageClass(storageClass).build();

// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload returns a 412 error if
// the object's generation number does not match your precondition.
Storage.BlobSourceOption precondition =
Storage.BlobSourceOption.generationMatch(storage.get(blobId).getGeneration());
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto


Storage.CopyRequest request =
Storage.CopyRequest.newBuilder()
.setSource(blobId)
.setTarget(BlobInfo.newBuilder(blobId).setStorageClass(storageClass).build())
.setSourceOptions(precondition) // delete this line to run without preconditions
.setTarget(targetBlob)
.build();
Blob updatedBlob = storage.copy(request).getResult();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,22 @@ public static void composeObject(

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

// 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.
// For a target object that does not yet exist, set the DoesNotExist precondition.
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();
Shabirmean marked this conversation as resolved.
Show resolved Hide resolved

Storage.ComposeRequest composeRequest =
Storage.ComposeRequest.newBuilder()
// addSource takes varargs, so you can put as many objects here as you want, up to the
// max of 32
.addSource(firstObjectName, secondObjectName)
.setTarget(BlobInfo.newBuilder(bucketName, targetObjectName).build())
.setTargetOptions(precondition)
.build();

Blob compositeObject = storage.compose(composeRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package com.example.storage.object;

// [START storage_copy_file]
import com.google.cloud.storage.Blob;

import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

Expand All @@ -34,14 +35,25 @@ public static void copyObject(
// String objectName = "your-object-name";

// The ID of the bucket to copy the object to
// String targetBucketName = "target-object-bucket"
// String targetBucketName = "target-object-bucket";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Blob blob = storage.get(sourceBucketName, objectName);
BlobId source = BlobId.of(sourceBucketName, objectName);
BlobId target =
BlobId.of(
targetBucketName, objectName); // you could change "objectName" to rename the object
Shabirmean marked this conversation as resolved.
Show resolved Hide resolved

// 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.
// For a target object that does not yet exist, set the DoesNotExist precondition.
Shabirmean marked this conversation as resolved.
Show resolved Hide resolved
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();

// This keeps the original name, you could also do
// copyTo(targetBucketName, "target-object-name") to change the name
blob.copyTo(targetBucketName);
storage.copy(
Storage.CopyRequest.newBuilder().setSource(source).setTarget(target, precondition).build());

System.out.println(
"Copied object "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,20 @@ public static void copyOldVersionOfObject(
// String newObjectName = "your-new-object";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

// 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.
// For a target object that does not yet exist, set the DoesNotExist precondition.
Shabirmean marked this conversation as resolved.
Show resolved Hide resolved
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();

Storage.CopyRequest copyRequest =
Storage.CopyRequest.newBuilder()
.setSource(BlobId.of(bucketName, objectToCopy, generationToCopy))
.setTarget(BlobId.of(bucketName, newObjectName))
.setTarget(BlobId.of(bucketName, newObjectName), precondition)
.build();
storage.copy(copyRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ public static void deleteObject(String projectId, String bucketName, String obje
// String objectName = "your-object-name";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
storage.delete(bucketName, objectName);

// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload returns a 412 error if
// the object's generation number does not match your precondition.
Storage.BlobSourceOption precondition =
Storage.BlobSourceOption.generationMatch(
storage.get(bucketName, objectName).getGeneration());
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto


storage.delete(bucketName, objectName, precondition);

System.out.println("Object " + objectName + " was deleted from " + bucketName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package com.example.storage.object;

// [START storage_move_file]

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.CopyWriter;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

Expand All @@ -45,13 +46,25 @@ public static void moveObject(
// String targetObjectName = "your-new-object-name";

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Blob blob = storage.get(sourceBucketName, sourceObjectName);
// Write a copy of the object to the target bucket
CopyWriter copyWriter = blob.copyTo(targetBucketName, targetObjectName);
Blob copiedBlob = copyWriter.getResult();
BlobId source = BlobId.of(sourceBucketName, sourceObjectName);
BlobId target = BlobId.of(targetBucketName, targetObjectName);

// 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.
// For a target object that does not yet exist, set the DoesNotExist precondition.
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
// If the destination already exists in your bucket, instead set a generation-match
// precondition:
Shabirmean marked this conversation as resolved.
Show resolved Hide resolved
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();

// Copy source object to target object
storage.copy(
Storage.CopyRequest.newBuilder().setSource(source).setTarget(target, precondition).build());
Blob copiedObject = storage.get(target);
// Delete the original blob now that we've copied to where we want it, finishing the "move"
// operation
blob.delete();
storage.get(source).delete();

System.out.println(
"Moved object "
Expand All @@ -61,7 +74,7 @@ public static void moveObject(
+ " to "
+ targetObjectName
+ " in bucket "
+ copiedBlob.getBucket());
+ copiedObject.getBucket());
}
}
// [END storage_move_file]
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ public static void rotateObjectEncryptionKey(

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);

// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload returns a 412 error if
// the object's generation number does not match your precondition.
Storage.BlobSourceOption precondition =
Storage.BlobSourceOption.generationMatch(storage.get(blobId).getGeneration());
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto


// You can't change an object's encryption key directly, the only way is to overwrite the object
Storage.CopyRequest request =
Storage.CopyRequest.newBuilder()
.setSource(blobId)
.setSourceOptions(Storage.BlobSourceOption.decryptionKey(oldEncryptionKey))
.setSourceOptions(
Storage.BlobSourceOption.decryptionKey(oldEncryptionKey), precondition)
.setTarget(blobId, Storage.BlobTargetOption.encryptionKey(newEncryptionKey))
.build();
storage.copy(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,21 @@ public static void uploadEncryptedObject(
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();

// 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.
// For a target object that does not yet exist, set the DoesNotExist precondition.
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();

storage.create(
blobInfo,
Files.readAllBytes(Paths.get(filePath)),
Storage.BlobTargetOption.encryptionKey(encryptionKey));
Storage.BlobTargetOption.encryptionKey(encryptionKey),
precondition);
Shabirmean marked this conversation as resolved.
Show resolved Hide resolved

System.out.println(
"File "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ public static void uploadKmsEncryptedObject(

BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
storage.create(blobInfo, data, Storage.BlobTargetOption.kmsKeyName(kmsKeyName));

// 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.
// For a target object that does not yet exist, set the DoesNotExist precondition.
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();
Shabirmean marked this conversation as resolved.
Show resolved Hide resolved

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

System.out.println(
"Uploaded object "
Expand Down