Skip to content

Commit 79b7cf0

Browse files
sydney-munrogcf-owl-bot[bot]BenWhitehead
authored
docs: Update DeleteObject Sample to be clearer on object versioning behavior (#2595)
* 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 <BenWhitehead@users.noreply.github.com> * 🦉 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 <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: BenWhitehead <BenWhitehead@users.noreply.github.com>
1 parent 60692c2 commit 79b7cf0

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

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

+14-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

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

@@ -38,16 +39,19 @@ public static void deleteObject(String projectId, String bucketName, String obje
3839
System.out.println("The object " + objectName + " wasn't found in " + bucketName);
3940
return;
4041
}
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);
49-
50-
System.out.println("Object " + objectName + " was deleted from " + bucketName);
42+
BlobId idWithGeneration = blob.getBlobId();
43+
// Deletes the blob specified by its id. When the generation is present and non-null it will be
44+
// specified in the request.
45+
// If versioning is enabled on the bucket and the generation is present in the delete request,
46+
// only the version of the object with the matching generation will be deleted.
47+
// If instead you want to delete the current version, the generation should be dropped by
48+
// performing the following.
49+
// BlobId idWithoutGeneration =
50+
// BlobId.of(idWithGeneration.getBucket(), idWithGeneration.getName());
51+
// storage.delete(idWithoutGeneration);
52+
storage.delete(idWithGeneration);
53+
54+
System.out.println("Object " + objectName + " was permanently deleted from " + bucketName);
5155
}
5256
}
5357
// [END storage_delete_file]

0 commit comments

Comments
 (0)