Skip to content

Commit

Permalink
samples: adding preconditions to some of the samples (#1718)
Browse files Browse the repository at this point in the history
* samples: adding preconditions to some of the samples

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>
  • Loading branch information
sydney-munro and gcf-owl-bot[bot] committed Oct 31, 2022
1 parent 33f231a commit 9ba14b7
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

// [START storage_release_event_based_hold]

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.StorageOptions;
Expand All @@ -38,8 +38,18 @@ public static void releaseEventBasedHold(String projectId, String bucketName, St

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

storage.update(BlobInfo.newBuilder(blobId).setEventBasedHold(false).build());
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.println("The object " + objectName + " was not found in " + bucketName);
return;
}

// 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.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();

blob.toBuilder().setEventBasedHold(false).build().update(precondition);

System.out.println("Event-based hold was released for " + objectName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

// [START storage_release_temporary_hold]

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.StorageOptions;
Expand All @@ -39,7 +39,18 @@ public static void releaseTemporaryHold(String projectId, String bucketName, Str
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

BlobId blobId = BlobId.of(bucketName, objectName);
storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(false).build());
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.println("The object " + objectName + " was not found in " + bucketName);
return;
}

// 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.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();

blob.toBuilder().setTemporaryHold(false).build().update(precondition);

System.out.println("Temporary hold was released for " + objectName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

// [START storage_set_event_based_hold]

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.StorageOptions;
Expand All @@ -38,8 +38,18 @@ public static void setEventBasedHold(String projectId, String bucketName, String

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

storage.update(BlobInfo.newBuilder(blobId).setEventBasedHold(true).build());
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.println("The object " + objectName + " was not found in " + bucketName);
return;
}

// 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.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();

blob.toBuilder().setEventBasedHold(true).build().update(precondition);

System.out.println("Event-based hold was set for " + objectName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// [START storage_set_metadata]

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.util.HashMap;
Expand All @@ -38,10 +39,21 @@ public static void setObjectMetadata(String projectId, String bucketName, String
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Map<String, String> newMetadata = new HashMap<>();
newMetadata.put("keyToAddOrUpdate", "value");
Blob blob = storage.get(bucketName, objectName);
BlobId blobId = BlobId.of(bucketName, objectName);
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.println("The object " + objectName + " was not found in " + bucketName);
return;
}

// 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.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();

// Does an upsert operation, if the key already exists it's replaced by the new value, otherwise
// it's added.
blob.toBuilder().setMetadata(newMetadata).build().update();
blob.toBuilder().setMetadata(newMetadata).build().update(precondition);

System.out.println(
"Updated custom metadata for object " + objectName + " in bucket " + bucketName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

// [START storage_set_temporary_hold]

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.StorageOptions;
Expand All @@ -38,8 +38,18 @@ public static void setTemporaryHold(String projectId, String bucketName, String

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

storage.update(BlobInfo.newBuilder(blobId).setTemporaryHold(true).build());
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.println("The object " + objectName + " was not found in " + bucketName);
return;
}

// 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.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();

blob.toBuilder().setTemporaryHold(true).build().update(precondition);

System.out.println("Temporary hold was set for " + objectName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,19 @@ public static void uploadObject(
// The path to your file to upload
// String filePath = "path/to/your/file"

// 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 storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)));
storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)), precondition);

System.out.println(
"File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
Expand Down

0 comments on commit 9ba14b7

Please sign in to comment.