From 79b7cf05326ea135c552cbeee1b97e7ff115189f Mon Sep 17 00:00:00 2001 From: Sydney Munro <97561403+sydney-munro@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:42:44 -0700 Subject: [PATCH] docs: Update DeleteObject Sample to be clearer on object versioning behavior (#2595) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs: Update DeleteObject Sample to be clearer on object versioning behavior * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update samples/snippets/src/main/java/com/example/storage/object/DeleteObject.java Co-authored-by: BenWhitehead * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * adding in imports * rephrase comments --------- Co-authored-by: Owl Bot Co-authored-by: BenWhitehead --- .../example/storage/object/DeleteObject.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/storage/object/DeleteObject.java b/samples/snippets/src/main/java/com/example/storage/object/DeleteObject.java index 7fcb8fed91..3e8d7d331f 100644 --- a/samples/snippets/src/main/java/com/example/storage/object/DeleteObject.java +++ b/samples/snippets/src/main/java/com/example/storage/object/DeleteObject.java @@ -18,6 +18,7 @@ // [START storage_delete_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; @@ -38,16 +39,19 @@ public static void deleteObject(String projectId, String bucketName, String obje System.out.println("The object " + objectName + " wasn't 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.BlobSourceOption precondition = - Storage.BlobSourceOption.generationMatch(blob.getGeneration()); - - storage.delete(bucketName, objectName, precondition); - - System.out.println("Object " + objectName + " was deleted from " + bucketName); + BlobId idWithGeneration = blob.getBlobId(); + // Deletes the blob specified by its id. When the generation is present and non-null it will be + // specified in the request. + // If versioning is enabled on the bucket and the generation is present in the delete request, + // only the version of the object with the matching generation will be deleted. + // If instead you want to delete the current version, the generation should be dropped by + // performing the following. + // BlobId idWithoutGeneration = + // BlobId.of(idWithGeneration.getBucket(), idWithGeneration.getName()); + // storage.delete(idWithoutGeneration); + storage.delete(idWithGeneration); + + System.out.println("Object " + objectName + " was permanently deleted from " + bucketName); } } // [END storage_delete_file]