Skip to content

Commit 4b3be44

Browse files
docs: Add preconditions to some samples (#1600)
* docs: Add preconditions to some samples * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * remove star import * add null checks * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent ab3a198 commit 4b3be44

10 files changed

+142
-22
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.storage.object;
1818

1919
// [START storage_object_csek_to_cmek]
20+
import com.google.cloud.storage.Blob;
2021
import com.google.cloud.storage.BlobId;
2122
import com.google.cloud.storage.Storage;
2223
import com.google.cloud.storage.StorageOptions;
@@ -47,10 +48,22 @@ public static void changeObjectFromCsekToKms(
4748

4849
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
4950
BlobId blobId = BlobId.of(bucketName, objectName);
51+
Blob blob = storage.get(blobId);
52+
if (blob == null) {
53+
System.out.println("The object " + objectName + " wasn't found in " + bucketName);
54+
return;
55+
}
56+
57+
// Optional: set a generation-match precondition to avoid potential race
58+
// conditions and data corruptions. The request to upload returns a 412 error if
59+
// the object's generation number does not match your precondition.
60+
Storage.BlobSourceOption precondition =
61+
Storage.BlobSourceOption.generationMatch(blob.getGeneration());
62+
5063
Storage.CopyRequest request =
5164
Storage.CopyRequest.newBuilder()
5265
.setSource(blobId)
53-
.setSourceOptions(Storage.BlobSourceOption.decryptionKey(decryptionKey))
66+
.setSourceOptions(Storage.BlobSourceOption.decryptionKey(decryptionKey), precondition)
5467
.setTarget(blobId, Storage.BlobTargetOption.kmsKeyName(kmsKeyName))
5568
.build();
5669
storage.copy(request);

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,32 @@ public static void changeObjectStorageClass(
3838

3939
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
4040
BlobId blobId = BlobId.of(bucketName, objectName);
41+
Blob sourceBlob = storage.get(blobId);
42+
if (sourceBlob == null) {
43+
System.out.println("The object " + objectName + " wasn't found in " + bucketName);
44+
return;
45+
}
4146

4247
// See the StorageClass documentation for other valid storage classes:
4348
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
4449
StorageClass storageClass = StorageClass.COLDLINE;
4550

4651
// You can't change an object's storage class directly, the only way is to rewrite the object
47-
// with the
48-
// desired storage class
52+
// with the desired storage class
53+
54+
BlobInfo targetBlob = BlobInfo.newBuilder(blobId).setStorageClass(storageClass).build();
55+
56+
// Optional: set a generation-match precondition to avoid potential race
57+
// conditions and data corruptions. The request to upload returns a 412 error if
58+
// the object's generation number does not match your precondition.
59+
Storage.BlobSourceOption precondition =
60+
Storage.BlobSourceOption.generationMatch(sourceBlob.getGeneration());
61+
4962
Storage.CopyRequest request =
5063
Storage.CopyRequest.newBuilder()
5164
.setSource(blobId)
52-
.setTarget(BlobInfo.newBuilder(blobId).setStorageClass(storageClass).build())
65+
.setSourceOptions(precondition) // delete this line to run without preconditions
66+
.setTarget(targetBlob)
5367
.build();
5468
Blob updatedBlob = storage.copy(request).getResult();
5569

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,22 @@ public static void composeObject(
4646

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

49+
// Optional: set a generation-match precondition to avoid potential race
50+
// conditions and data corruptions. The request returns a 412 error if the
51+
// preconditions are not met.
52+
// For a target object that does not yet exist, set the DoesNotExist precondition.
53+
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
54+
// If the destination already exists in your bucket, instead set a generation-match
55+
// precondition:
56+
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
57+
4958
Storage.ComposeRequest composeRequest =
5059
Storage.ComposeRequest.newBuilder()
5160
// addSource takes varargs, so you can put as many objects here as you want, up to the
5261
// max of 32
5362
.addSource(firstObjectName, secondObjectName)
5463
.setTarget(BlobInfo.newBuilder(bucketName, targetObjectName).build())
64+
.setTargetOptions(precondition)
5565
.build();
5666

5767
Blob compositeObject = storage.compose(composeRequest);

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package com.example.storage.object;
1818

1919
// [START storage_copy_file]
20-
import com.google.cloud.storage.Blob;
20+
21+
import com.google.cloud.storage.BlobId;
2122
import com.google.cloud.storage.Storage;
2223
import com.google.cloud.storage.StorageOptions;
2324

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

3637
// The ID of the bucket to copy the object to
37-
// String targetBucketName = "target-object-bucket"
38+
// String targetBucketName = "target-object-bucket";
3839

3940
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
40-
Blob blob = storage.get(sourceBucketName, objectName);
41+
BlobId source = BlobId.of(sourceBucketName, objectName);
42+
BlobId target =
43+
BlobId.of(
44+
targetBucketName, objectName); // you could change "objectName" to rename the object
45+
46+
// Optional: set a generation-match precondition to avoid potential race
47+
// conditions and data corruptions. The request returns a 412 error if the
48+
// preconditions are not met.
49+
// For a target object that does not yet exist, set the DoesNotExist precondition.
50+
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
51+
// If the destination already exists in your bucket, instead set a generation-match
52+
// precondition:
53+
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
4154

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

4658
System.out.println(
4759
"Copied object "

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,20 @@ public static void copyOldVersionOfObject(
4444
// String newObjectName = "your-new-object";
4545

4646
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
47+
48+
// Optional: set a generation-match precondition to avoid potential race
49+
// conditions and data corruptions. The request returns a 412 error if the
50+
// preconditions are not met.
51+
// For a target object that does not yet exist, set the DoesNotExist precondition.
52+
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
53+
// If the destination already exists in your bucket, instead set a generation-match
54+
// precondition:
55+
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
56+
4757
Storage.CopyRequest copyRequest =
4858
Storage.CopyRequest.newBuilder()
4959
.setSource(BlobId.of(bucketName, objectToCopy, generationToCopy))
50-
.setTarget(BlobId.of(bucketName, newObjectName))
60+
.setTarget(BlobId.of(bucketName, newObjectName), precondition)
5161
.build();
5262
storage.copy(copyRequest);
5363

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.storage.object;
1818

1919
// [START storage_delete_file]
20+
import com.google.cloud.storage.Blob;
2021
import com.google.cloud.storage.Storage;
2122
import com.google.cloud.storage.StorageOptions;
2223

@@ -32,7 +33,19 @@ public static void deleteObject(String projectId, String bucketName, String obje
3233
// String objectName = "your-object-name";
3334

3435
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
35-
storage.delete(bucketName, objectName);
36+
Blob blob = storage.get(bucketName, objectName);
37+
if (blob == null) {
38+
System.out.println("The object " + objectName + " wasn't found in " + bucketName);
39+
return;
40+
}
41+
42+
// Optional: set a generation-match precondition to avoid potential race
43+
// conditions and data corruptions. The request to upload returns a 412 error if
44+
// the object's generation number does not match your precondition.
45+
Storage.BlobSourceOption precondition =
46+
Storage.BlobSourceOption.generationMatch(blob.getGeneration());
47+
48+
storage.delete(bucketName, objectName, precondition);
3649

3750
System.out.println("Object " + objectName + " was deleted from " + bucketName);
3851
}

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
package com.example.storage.object;
1818

1919
// [START storage_move_file]
20+
2021
import com.google.cloud.storage.Blob;
21-
import com.google.cloud.storage.CopyWriter;
22+
import com.google.cloud.storage.BlobId;
2223
import com.google.cloud.storage.Storage;
2324
import com.google.cloud.storage.StorageOptions;
2425

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

4748
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
48-
Blob blob = storage.get(sourceBucketName, sourceObjectName);
49-
// Write a copy of the object to the target bucket
50-
CopyWriter copyWriter = blob.copyTo(targetBucketName, targetObjectName);
51-
Blob copiedBlob = copyWriter.getResult();
49+
BlobId source = BlobId.of(sourceBucketName, sourceObjectName);
50+
BlobId target = BlobId.of(targetBucketName, targetObjectName);
51+
52+
// Optional: set a generation-match precondition to avoid potential race
53+
// conditions and data corruptions. The request returns a 412 error if the
54+
// preconditions are not met.
55+
// For a target object that does not yet exist, set the DoesNotExist precondition.
56+
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
57+
// If the destination already exists in your bucket, instead set a generation-match
58+
// precondition:
59+
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
60+
61+
// Copy source object to target object
62+
storage.copy(
63+
Storage.CopyRequest.newBuilder().setSource(source).setTarget(target, precondition).build());
64+
Blob copiedObject = storage.get(target);
5265
// Delete the original blob now that we've copied to where we want it, finishing the "move"
5366
// operation
54-
blob.delete();
67+
storage.get(source).delete();
5568

5669
System.out.println(
5770
"Moved object "
@@ -61,7 +74,7 @@ public static void moveObject(
6174
+ " to "
6275
+ targetObjectName
6376
+ " in bucket "
64-
+ copiedBlob.getBucket());
77+
+ copiedObject.getBucket());
6578
}
6679
}
6780
// [END storage_move_file]

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.storage.object;
1818

1919
// [START storage_rotate_encryption_key]
20+
import com.google.cloud.storage.Blob;
2021
import com.google.cloud.storage.BlobId;
2122
import com.google.cloud.storage.Storage;
2223
import com.google.cloud.storage.StorageOptions;
@@ -48,11 +49,24 @@ public static void rotateObjectEncryptionKey(
4849

4950
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
5051
BlobId blobId = BlobId.of(bucketName, objectName);
52+
Blob blob = storage.get(blobId);
53+
if (blob == null) {
54+
System.out.println("The object " + objectName + " wasn't found in " + bucketName);
55+
return;
56+
}
57+
58+
// Optional: set a generation-match precondition to avoid potential race
59+
// conditions and data corruptions. The request to upload returns a 412 error if
60+
// the object's generation number does not match your precondition.
61+
Storage.BlobSourceOption precondition =
62+
Storage.BlobSourceOption.generationMatch(blob.getGeneration());
63+
5164
// You can't change an object's encryption key directly, the only way is to overwrite the object
5265
Storage.CopyRequest request =
5366
Storage.CopyRequest.newBuilder()
5467
.setSource(blobId)
55-
.setSourceOptions(Storage.BlobSourceOption.decryptionKey(oldEncryptionKey))
68+
.setSourceOptions(
69+
Storage.BlobSourceOption.decryptionKey(oldEncryptionKey), precondition)
5670
.setTarget(blobId, Storage.BlobTargetOption.encryptionKey(newEncryptionKey))
5771
.build();
5872
storage.copy(request);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,21 @@ public static void uploadEncryptedObject(
4848
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
4949
BlobId blobId = BlobId.of(bucketName, objectName);
5050
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
51+
52+
// Optional: set a generation-match precondition to avoid potential race
53+
// conditions and data corruptions. The request returns a 412 error if the
54+
// preconditions are not met.
55+
// For a target object that does not yet exist, set the DoesNotExist precondition.
56+
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
57+
// If the destination already exists in your bucket, instead set a generation-match
58+
// precondition:
59+
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
60+
5161
storage.create(
5262
blobInfo,
5363
Files.readAllBytes(Paths.get(filePath)),
54-
Storage.BlobTargetOption.encryptionKey(encryptionKey));
64+
Storage.BlobTargetOption.encryptionKey(encryptionKey),
65+
precondition);
5566

5667
System.out.println(
5768
"File "

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,17 @@ public static void uploadKmsEncryptedObject(
4545

4646
BlobId blobId = BlobId.of(bucketName, objectName);
4747
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
48-
storage.create(blobInfo, data, Storage.BlobTargetOption.kmsKeyName(kmsKeyName));
48+
49+
// Optional: set a generation-match precondition to avoid potential race
50+
// conditions and data corruptions. The request returns a 412 error if the
51+
// preconditions are not met.
52+
// For a target object that does not yet exist, set the DoesNotExist precondition.
53+
Storage.BlobTargetOption precondition = Storage.BlobTargetOption.doesNotExist();
54+
// If the destination already exists in your bucket, instead set a generation-match
55+
// precondition:
56+
// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
57+
58+
storage.create(blobInfo, data, Storage.BlobTargetOption.kmsKeyName(kmsKeyName), precondition);
4959

5060
System.out.println(
5161
"Uploaded object "

0 commit comments

Comments
 (0)